string and string builder

For a simple string concantenation code shown below it will create 3 copies of string in memory.

//The below line of code Creates one copy of the string
string str = “shiv”;

/* The below line of code creates three copies of string object one for the concatenation at right hand side and the other for new value at the left hand side. The first old allocated memory is sent for garbage collection.*/
str = str + “shiv”;

When you use the string builder for concatenation it will create only one copy of the object.

/* The below code uses string builder and only one
object is created as compared to normal string where we have 3 copies created*/
StringBuilder objBuilder = new StringBuilder();
objBuilder.Append(“shiv”);

Ok now summarizing what should we say to the interviewer.

String is immutable. Immutable means once assigned it can not be changed. Thats why it creates more copies of the object as it can not use the same instance.String builder is mutable , in other words the same object will changed rather than creating new objects.

Advertisements

Boxing and Unboxing

Converting a value type to reference type is called Boxing.
Unboxing is the opposite operation
Boxing confirm the conversion of value type variable or literals to the reference type

int i = 12;
object box = i;
if (box is int) {
Console.Write(“Box contains an int”);
}
————————————————–

struct Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);

will output the value 10 on the console
—————————————-
Unboxing:

object box = 12;
int i = (int)box;
object box = new int_Box(12);
int i = ((int_Box)box).value;

Constructor and its Types

constructor is a class method that gets automatically executed whenever class’s object is created or whenever class is initialized

– Constructor does not have any return type
– Construtctor have the same name of the class

1.Instance or Default Constructor:——–
Constructor called implicitly when we create the class instance, the modifiers can be public, private, protected, and internal or protected internal.

2. static Constructor——— will call only once when the class gets loaded in the memory without creating the instance of the class.
– It can only access the static members of the class.
– There can be only one static constructor in the class.
– The static constructor should be without parameters.

3. Parameterized Constructors:———-
will require initializing class members during instantiation
– same rules as default constructor and will have parameters

4. A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

RenderPage and RenderBody

RenderPage
RenderPage method also exists in the Layout page to render other page exists in your application. A layout page can have multiple RenderPage method.
@RenderPage(“~/Views/Shared/_Header.cshtml”)

RenderBody
RenderBody method exists in the Layout page to render child page/view. It is just like the ContentPlaceHolder in master page. A layout page can have only one RenderBody method.
@RenderBody()

Return types of a controller action method

The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.

ActionResult Helper Method Description
EmptyResult (None) returns a null result
FileResult File Returns a binary output to write to the response
JavaScriptResult JavaScript Returns a script that can be executed on the client
JsonResult Json When you want to return a serialized JSON object
ContentResult Content Returns a user-defined content type
RedirectToRouteResult RedirectToRoute Redirect to another action method
RedirectResult Redirect When you want to redirect to another action method we will use RedirectResult
PartialViewResult PartialView As the name describe PartialViewResult renders the partial view.
ViewResult View ViewResult Renders a view as a web page.

PartialView As the name describe PartialViewResult renders the partial view.
RedirectResult Redirect When you want to redirect to another action method we will use RedirectResult
RedirectToRouteResult RedirectToRoute Redirect to another action method
ContentResult Content Returns a user-defined content type
JsonResult Json When you want to return a serialized JSON object
JavaScriptResult JavaScript Returns a script that can be executed on the client
FileResult File Returns a binary output to write to the response
EmptyResult (None) returns a null result

ng-hide directive and ng-click directive

ng-hide directive hides a given control.

In below example, we’ve added ng-hide attribute to a HTML button and pass it a model. Then we’ve attached the model to a checkbox and can see the variation.

<input type = “checkbox” ng-model = “showHide2”>Hide Button
<button ng-hide = “showHide2”>Click Me!</button>

ng-click directive represents a AngularJS click event.

In below example, we’ve added ng-click attribute to a HTML button and added an expression to updated a model. Then we can see the variation.

<p>Total click: {{ clickCounter }}</p></td>
<button ng-click = “clickCounter = clickCounter + 1”>Click Me!</button>

Factories Vs Service

Factories are functions that return the object, while services are constructor functions of the object which are instantiated with the new keyword.

Factory can return anything which can be a class(constructor function), instance of class, string, number or boolean. If you return a constructor function, you can instantiate in your controller.

Example:

myApp.factory(‘myFactory’, function () {

// any logic here..

// Return any thing. Here it is object
return {
name: ‘Joe’
}
}

If you’re using an object, you could use the factory provider.

Service does not need to return anything. But you have to assign everything in this variable. Because service will create instance by default and use that as a base object.

myApp.service(‘myService’, function () {

// any logic here..

this.name = ‘Joe’;
}
If you’re using a class you could use the service provider