1. The following is a structure definition:
public struct Point{public int X { get; set; }public int Y { get; set; }}
When to use the structure:
- Small Data Structures
- The value is generally not modified.
2. Class Definition:
public class Animal{public string Name { get; set; }public double Weight { get; private set; }private string _color;public string Color{get { return _color; }set { _color = value; }}public void MakeSound(){Console.WriteLine(Sound); }}
3. The difference between the structure and the class: no explicit constructor is available in the above two cases. If you add an explicit constructor to them:
public struct Point{public int X { get; set; }public int Y { get; set; }public Point(int X, int Y){this.X = X;this.Y = Y;}}
If we instantiate it, we can find that the implicit constructor is still available:
Point p = new Point();Point P = new Point(10, 12);
At the same time, we cannot define a non-parameter constructor in the structure. For classes, if we do not write any constructor for the class, the C # compiler automatically generates a non-parameter constructor for the class during compilation, but once we write any constructor for this class, this implicit constructor will not be automatically generated.