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:
The values in a small data structure are 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.