struct

 class Program

    {

        static void Main(string[] args)

        {

            Point p = new Point(1, 1);

            Console.WriteLine(p);

 

            p.Change(2, 2);

            Console.WriteLine(p);

 

            object o = p;

            Console.WriteLine(o);

 

            ((Point)o).Change(3, 3);

            Console.WriteLine(o);

 

            p = (Point)o;

            p.Change(3, 3);

            Console.WriteLine(p);

        }

    }

 

    internal struct Point

    {

        Int32 m_x, m_y;

 

        public Point(Int32 x, Int32 y)

        {

            m_x = x;

            m_y = y;

        }

 

        public void Change(Int32 x, Int32 y)

        {

            m_x = x;

            m_y = y;

        }

 

        public override string ToString()

        {

            return string.Format("({0},{1})", m_x, m_y);

        }

    }

         输出:

 总结:CLR Via C#(第五章):值类型与引用类型——装箱和拆箱

Class

如果将Point改为Class类型,则输出:

总结:CLR Via C#(第五章):值类型与引用类型——装箱和拆箱
 

Interface

    class Program

    {

        static void Main(string[] args)

        {

            Point p = new Point(1, 1);

            Console.WriteLine(p);

 

            p.Change(2, 2);

            Console.WriteLine(p);

 

            object o = p;

            Console.WriteLine(o);

 

            ((Point)o).Change(3, 3);

            Console.WriteLine(o);

 

            p = (Point)o;

            p.Change(3, 3);

            Console.WriteLine(p);

 

 

            ((IChangeBoxedPoint)p).Change(4, 4);

            Console.WriteLine(p);

 

            ((IChangeBoxedPoint)o).Change(5, 5);

            Console.WriteLine(o);

 

            Console.ReadLine();

        }

    }

 

    internal interface IChangeBoxedPoint

    {

        void Change(Int32 x, Int32 y);

    }

 

    internal struct Point:IChangeBoxedPoint

    {

        Int32 m_x, m_y;

 

        public Point(Int32 x, Int32 y)

        {

            m_x = x;

            m_y = y;

        }

 

        public void Change(Int32 x, Int32 y)

        {

            m_x = x;

            m_y = y;

        }

 

        public override string ToString()

        {

            return string.Format("({0},{1})", m_x, m_y);

        }

    }

 

输出:

 总结:CLR Via C#(第五章):值类型与引用类型——装箱和拆箱

相关文章:

  • 2021-07-16
  • 2021-09-23
  • 2022-12-23
  • 2021-06-16
  • 2021-12-03
  • 2021-10-22
  • 2019-09-24
  • 2021-09-17
猜你喜欢
  • 2021-06-07
  • 2022-01-13
  • 2021-09-19
  • 2021-07-04
  • 2021-08-11
  • 2022-12-23
相关资源
相似解决方案