SoFunction
Updated on 2025-03-06

Deeply discuss the structural struct in C#

1. The difference between structure and class

1. The level of the structure is the same as the class. It is written under the namespace. You can define fields, properties, methods, and construct methods. You can also create objects through the keyword new.

2. Fields in the structure cannot be assigned initial values.

3. The C# compiler will automatically generate anyway, so a parameterless constructor cannot be defined for the structure.

4. In the constructor, all fields of the structure must be assigned values.

5. In the constructor, assigning values ​​to attributes is not considered as assignments to fields, because attributes do not necessarily operate on fields.

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

7. The automatic attribute cannot be defined because the field attribute will generate a field, and this field must be required in the constructor, but we do not know what the field is called.

8. When declaring a structure object, you can not use the new keyword, but at this time, the fields of the structure object do not have an initial value. Because the constructor is not called, the field must be assigned to the field. Therefore, if the structure object is created through the new keyword, the fields of this object have a default value.

9. The stack access speed is fast, but the space is small, the heap access speed is slow, but the space is large. When we want to represent a lightweight object, it is defined as a structure to improve the speed and choose according to the impact of the transmission. If we want to pass a reference, it is defined as a class, and if we pass a copy, it is defined as a structure.

2. Demo

Copy the codeThe code is as follows:

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()
        {
            ("X={0},Y={1}", , );
        }
        public Point(int x,int y)
        {
            = x;
            = y;
            = null;
        }
        public Point(int x)
        {
            = x;
            = 11;
            = null;
        }
        public Point(int x, int y, Program p)
        {
            = x;
            = y;
            = p;
        }
    }
    class Program
    {
        public string Name { get; set; }
        static void Main(string[] args)
        {
            //Point p = new Point();
            // = 120;
            // = 100;
            //Point p1 = p;
            // = 190;
            //();

            //Point p;
// = 12;// If you don't assign a value, you will report an error
            //();
            //Point p1 = new Point();
//();//No value is assigned here, there will be no errors, see the difference between the reasons 8

Program p = new Program() { Name="Xiaohua"};
            Point point1 = new Point(10, 10, p);
            Point point2 = point1;
= "Xiao Ming";
();//The result is Xiao Ming, see the following figure for analysis
        }
    }