Why need boxing and unboxing

Why do we need boxing and unboxing in C#?
http://stackoverflow.com/questions/2111857/why-do-we-need-boxing-and-unboxing-in-c

 

struct Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return String.Format(“{0} {1}”, this.x, this.y);
}
}
class Point2
{
public int x, 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);
}
class Program
{
static void Print(object o)
{
Console.WriteLine(o);
}
static void Main(string[] args)
{
int x = 4;
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는 서로 다른 메모리에 데이타 복사본 저장)
Point p3 = new Point(1, 1);
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 
Console.WriteLine(((Point)o3).x); // 1  unboxing(object=>struct) o3.x=1 (o3는 박싱된 복사본 p3에의해 데이터 안바뀜)
Point2 p4 = new Point2(1, 1);
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에의해 데이터가 바뀜)
Console.WriteLine(p4); // p4.ToString()호출 4 1
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *