Copy a struct instance by assignment : Struct copy : Struct C# Examples


C# Examples » Struct » Struct copy »

 

Copy a struct instance by assignment









    
using  System;

        public  struct  Point
        {
                public  int  x;
                public  int  y;

                public  Point(  int  x,  int  y  )
                {
                        this.x  =  x;
                        this.y  =  y;
                }

                public  void  Print()
                {
                        System.Console.WriteLine(  "x  =  {0},  y  =  {1}",  x,  y  );
                }
        }

        class  MainClass
        {
                static  void  Main(string[]  args)
                {
                        Point  p  =  new  Point(  3,  4  );

                        Point  q  =  p;        //  takes  a  copy

                        q.x  =  5;                //  only  changes  q  not  p

                        p.Print();            //  so  we  still  see  x  =  3,  y  =  4
                }
        }
    
   
  
   



Output

x = 3, y = 4


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Struct
» Struct copy