一个关于boxing和unboxing的demo

demo 来自 CLR via C#(第三版)

 

View Code
 1 static void Main(string[] args)
 2         {
 3             Point p = new Point(1, 1);
 4             Console.WriteLine(p);
 5 
 6             p.Change(2, 2);
 7             Console.WriteLine(p);
 8 
 9             Object o = p;
10             Console.WriteLine(o);
11 
12             ((Point)o).Change(3, 3);
13             Console.WriteLine(o);
14 
15             ((IChangeBoxedPoint)p).Change(4, 4);
16             Console.WriteLine(p);
17 
18             ((IChangeBoxedPoint)o).Change(5, 5);
19             Console.WriteLine(o);
20          }
View Code
 1     public interface IChangeBoxedPoint
 2     {
 3         void Change(Int32 x, Int32 y);
 4     }
 5 
 6     public struct Point : IChangeBoxedPoint
 7     {
 8         private Int32 m_x, m_y;
 9         public Point(Int32 x, Int32 y)
10         {
11             m_x = x;
12             m_y = y;
13         }
14 
15         public void Change(Int32 x, Int32 y)
16         {
17             m_x = x;
18             m_y = y;
19         }
20 
21         public override string ToString()
22         {
23             return string.Format("({0},{1})", m_x, m_y);
24         }
25     }

 

 

posted on 2012-08-20 10:40  一根葛根  阅读(246)  评论(0编辑  收藏  举报