Why do we need boxing and unboxing in C#?
http://stackoverflow.com/questions/2111857/why-do-we-need-boxing-and-unboxing-in-c
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
{
return String.Format(“{0} {1}”, this.x, this.y);
}
{
public Point2(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return String.Format(“{0} {1}”, this.x, this.y);
}
{
{
Console.WriteLine(o);
}
{
Print(x); // boxing (int=>object)
double y = 3.2;
Print(y); // boxing (double=>object)
char z = ‘n’;
Print(z); // boxing (char=>object)
double e = 2.718281828459045;
object o1 = e; // boxing (double=>object)
object o2 = e; // boxing (double=>object)
Console.WriteLine(o1 == o2); // False (o1과 o2는 서로 다른 메모리에 데이타 복사본 저장)
Print(p3); // boxing (struct=>object)
object o3 = p3; // boxing (struct=>object)
p3.x = 2;
//((Point)o3).x = 4; // CS0445 Cannot modify the result of an unboxing conversion
Print(p4); // boxing 아님
object o4 = p4; // boxing 아님
p4.x = 2;
Print(p4); // upcasting(Point2 class=>object) 2 1
((Point2)o4).x = 4; // downcasting(object=>Point2 class) o4.x=2 (class는 reference type이므로 o4는 p4에의해 데이터가 바뀜)