public struct t1In this example (Download Example 4_1) x is a struct and is passed into AddFive. If x.Total was 0 upon entry then AddFive() will return 5 but x.Total will still be 0. Now change t1 to a class and because it is a reference type, x.Total will be 5 when AddFive() exits.
{
public int Total;
}
public static int AddFive( t1 x)
{
x.Total += 5;
return x.Total;
}
Boxing
Originally from Java, the concept of boxing is about storing a value type variable in a reference variable.int i = 99;This wraps the variable i in an object t i.e. inside a box. Remember all types ultimately derive from System.object.
object t=(object)i;
Console.WriteLine("Value of t ={0}",(int)t) ;
Console.WriteLine("Value of t ={0}",t) ;
Console.ReadKey() ;
Note - Boxing is a relatively slow process. According to Microsoft it takes 20 times as long as an assignment. This is because an object has to be created, then the valuetype variable copied into it. The opposite is unboxing, which is about four times as slow as assignment.
When would You Use Boxing?
The answer is you wouldn't if you can help it. If C# 1.0 and 1.1, it made possible null value value types (by boxing them as reference types) but with the nullable types in .NET 2.0 that is no longer needed.On the next page Static and Instance Classes

