Pages

Ads 468x60px

For New Update Use this Link http://dotnethubs.blogspot.in/ Offer for you

.

Showing posts with label example of boxing and unboxing in asp.net c#.Boxing. Show all posts
Showing posts with label example of boxing and unboxing in asp.net c#.Boxing. Show all posts

Friday, 28 June 2013

boxing and unboxing in c#

boxing and unboxing in c#
Boxing :-

  means converting value-type to reference-type.

Eg:

int I = 20;

string s = I.ToSting();

UnBoxing :- 
 means converting reference-type to value-type.

Eg:

int I = 20;

string s = I.ToString(); //Box the int

int J = Convert.ToInt32(s); //UnBox it back to an int.


Note: Performance Overheads due to boxing and unboxing as the boxing makes a copy of value type from stack and place it inside an object of type System.Object in the heap.

Value Type :-
As name suggest Value Type stores “value” directly.
For eg: 


//I and J are both of type int
I = 20;
J = I;

int is a value type, which means that the above statements will results in two locations in memory.
For each instance of value type separate memory is allocated.Stored in a Stack.
It Provides Quick Access, because of value located on stack.
 Reference Type : -

As name suggest Reference Type stores “reference” to the value.
For eg: 


Vector X, Y; //Object is defined. (No memory is allocated.)
X = new Vector(); //Memory is allocated to Object. //(new is responsible for allocating memory.)
X.value = 40; //Initialising value field in a vector class.
Y = X; //Both X and Y points to same memory location. //No memory is created for Y.
Console.writeline(Y.value); //displays 40, as both points to same memory
Y.value = 60;
Console.writeline(X.value); //displays 60.
Note: If a variable is reference it is possible to indicate that it does not refer to any object by setting its value to null;

Reference type are stored on Heap.
It provides comparatively slower access, as value located on heap.

 

..




New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result