CaptaiNdotNeT

Hello, and first of anything please forgive me if my question is very basic, i'm new to the c# programming, and to programming also, and i'm reading inside c# book, but i dont understand:
what is the objective of the boxing and unboxing methods, and what are the differences between have a value type and a reference type
i mean what can i do whit boxing, that i can't do whit out it.

thanks.

Randolf Rincon
Bucaramanga, Colombia.


Re: Visual C# Language C# Boxing & Unboxing

Saurabh Nandu

Randolf, I am going to try to give you a simplified explaination rather than get into too much details.

In C# there are two kinds of Types
1) Value Types - These consist of user defined struct, enum and other premetive types like int, long, short, bool etc. Value types are types that directly hold their data. Hence when ever you make a copy of the Value Type the actual data gets copied.

Example:



int i = 10 ;
int j = i ; // Copy the actual data
j = 12; // Change the value of J
Console.WriteLine("The value of i is {0} and the value of j is {1}",i,j);

// Output
//The value of i is 10 and the value of j is 12

 

Since the actual data got copied any changes made to j does not affect i.


2) Reference Types - These consist of user defined class, delegates etc. Reference types do not hold the data, they hold a reference (pointer) to the data. When you make a copy of reference types, the reference (pointer) is copied but the data remains the same.

Example:


Uri i = new Uri("//www.contoso.com/" href="http://www.contoso.com/">http://www.contoso.com/"); // Create a new Uri class
Uri j = i ; // Copy the reference of i into j
j = new Uri("http://www.mastercsharp.com"); // Change j's reference to a new Uri
Console.WriteLine("The value of i is {0} and the value of j is {1}",i,j);

// Output
//The value of i is http://www.mastercsharp.com and the value of j is //http://www.mastercsharp.com

 

Since the reference got copied both i and j point to the same data. Any change to the data will affect both the variables.


3) Boxing / Unboxing
The concept of boxing/unboxing happens internally and you don't need to do anything special for it. This concept has been introduced to act as a bridge between Value Types and Reference Types.
The idea is being able to store a Value type's data into a Reference type (boxing) and retriving the data back from the reference type into a value type (unboxing)

Example


int i = 10 ;
object obj = i ; // Boxing i (a value type)
int j = (int)obj ; // unboxing tha data from obj (a reference type)
// Note the explicit cast required while unboxing

Console.WriteLine("The value of i is {0} and the value of j is {1}",i,j);

// Output
//The value of i is 10 and the value of j is 10


 


When a value type is boxed the runtime need to copy the data, create a new reference to the data and then assign it to the object.
While unboxing the data is copied to the value type. Since the runtime needs to do these steps internally, the process of boxing and unboxing is said to be costly.

Hope this helps!






Re: Visual C# Language C# Boxing & Unboxing

Anonymous

CaptaiNdotNeT wrote:
Hello, and first of anything please forgive me if my question is very basic, i'm new to the c# programming, and to programming also, and i'm reading inside c# book, but i dont understand:
what is the objective of the boxing and unboxing methods, and what are the differences between have a value type and a reference type
i mean what can i do whit boxing, that i can't do whit out it.

thanks.

Randolf Rincon
Bucaramanga, Colombia.




Re: Visual C# Language C# Boxing & Unboxing

Developr

Hi,

Boxing and Unboxing aren't methods you explicitly have to call, but conversion of a value-type to reference type is valled 'boxing' and from reference type to value-type is called 'unboxing'. It's done behind the scenes.

One more thing to note is that you can 'unbox' only a variable which has been boxed before e.g.

int i = 100;

object o = (object)i; // boxing

i = (int) o //unboxing

Hope that helps.