Friday, August 29, 2008

Indexer

//>> Declaration
class Cars
{
private string[] _cars;
public string this[int index]
{
get
{
return _cars[index];
}
set
{
_cars[index] = value;
}
}
public void Demo()
{
Cars cars = new Cars();
cars[0] = "BMW";
cars[2] = "Lexus";
}
}

//>> Boxing
class Boxing
{
public void Demo()
{
int i = 1;
//box
object box = i;
//unbox
int j = (int)box;
}
}
// "int" is an alias of Int32 (System.Int32), which is System.ValueType.
// object, a heap based reference type as opposed to value type variable, which is stack allocated.

No comments: