Boxing/unboxing and Performance ?
The conversion from a value type to a reference type is known as boxing. Boxing happens automatically if a method requires an object as a parameter and a value type is passed. On the other side, a boxed value type can be converted to a value type by using unboxing. With unboxing, the cost operator is required.
var list = new ArrayList();
list.Add(44); //boxing, convert a value type to a reference type.
int i1 = (int)list[0]; //unboxing, convert a reference type to a value type.
foreach(int i2 in list)
{
Console.WriteLine(i2);
}
Note: Boxing and unboxing are easy to use but have a big performance impact, especially when iterating through many times.
Instead of using ArrayList, use the List<T> class from the namespace System.Collections.Generic
var list = new List<int>();
list.Add(44);
int i1 = list[0];
foreach(int i2 in list)
{
Console.WriteLine(i2);
}
Comments are Welcome.