Explore the structure struct in C

  • 2020-05-19 05:35:56
  • OfStack

1. Difference between structure and class

1, the structure level and class 1, written under the namespace, you can define fields, properties, methods, constructors can also create objects through the keyword new.

2. The field in the structure cannot be assigned an initial value.

3. No-argument constructors are automatically generated by the C# compiler anyway, so you cannot define a no-argument constructor for a structure.

4. In the constructor, you must assign values to all the fields of the structure.

5, in the constructor, assign a value to the property, is not considered to be assigned to the field, because the property does not 1 must be to manipulate the field.

6. Structure is a value type. When passing the structure variable, it will copy each field in the structure object into the field of the new structure variable.

7. You cannot define an automatic property, because the field property generates a field that must be in the constructor, but we do not know the name of the field.

Declare the struct object, you can not use the new keyword, but at this time, the field of the struct object has no initial value, because there is no call to the constructor, the constructor must assign a value for the field, so, through the new keyword to create a struct object, the field of this object has a default value.

9, stack access speed is fast, but the space is small, heap access speed is slow, but the space is large, when we want to represent a lightweight object, we will define as structure, in order to improve the speed, according to the impact of the transmission to select, want to transfer reference, then defined as class, transfer copy, then defined as structure.

2. Demo


struct Point
    {
        public Program p;
        private int x;
        public int X
        {
            get { return x; }
            set { x = value; }
        }
        private int y;
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public void Show()
        {
            Console.Write("X={0},Y={1}", this.X, this.Y);
        }
        public Point(int x,int y)
        {
            this.x = x;
            this.y = y;
            this.p = null;
        }
        public Point(int x)
        {
            this.x = x;
            this.y = 11;
            this.p = null;
        }
        public Point(int x, int y, Program p)
        {
            this.x = x;
            this.y = y;
            this.p = p;
        }
    }
    class Program
    {
        public string Name { get; set; }
        static void Main(string[] args)
        {
            //Point p = new Point();
            //p.X = 120;
            //p.Y = 100;
            //Point p1 = p;
            //p1.X = 190;
            //Console.WriteLine(p.X);
            //Point p;
            //p.X = 12;// No assignment will result in an error 
            //Console.WriteLine(p.X);
            //Point p1 = new Point();
            //Console.WriteLine(p1.X);// There is no error in not assigning a value here, for different reasons 8
            Program p = new Program() { Name=" The little flower "};
            Point point1 = new Point(10, 10, p);
            Point point2 = point1;
            point2.p.Name = " Xiao Ming ";
            Console.WriteLine(point1.p.Name);// The result is xiaoming. See the following figure for analysis 
        }
    }


Related articles: