Enum Season {spring=0,summer=22,fall,autumn=fall,winter=4}
Season colorful = Season.fall;
String name = Colorful. ToString ();
Console.WriteLine (colorful);//Output Fall
Console.WriteLine (name);//Output Fall
Console.WriteLine (int) colorful);//Output 23, here Note 23 is because summer 22 plus 1
Enumeration each element is associated (corresponding) with an integer value. is called the base integer value, or the underlying value.
If you do not specify a constant integer value for the enumeration literal constant, the compiler automatically assigns it a value that is 1 larger than the previous enumerated literal constant.
The first enumeration literal constant specifies that the default value is 0
It is important to note that multiple enumerated literal constants can have the same underlying values.
To save memory, you can choose
Enum Season:short {Spring,summer,fall,winter}
A type can be based on any of 8 integer types: Byte,sbyte,short,ushort,int,uint,long,ulong Eight
A mathematical operation can be performed on an enumeration, but if the result of the operation exceeds the value range of the enumeration definition, the only thing "runtime" can do is to interpret the value of the variable as the corresponding integer value.
//--------------------------------------------------
Structure (struct): When a class contains very little data, the heap is not cost-effective, so it is stored on the stack (stack), which is defined as struct (struct)
Tips
string s = "42";
int i = Int. Parse (s);//int. Parse (String) is the way to convert a string to int
Most of the time, do not declare a structure field as a public field! Otherwise, there is no guarantee that the public field always contains valid values.
The difference between a construct and a class:
① cannot declare a default constructor for a struct (a parameterless constructor).
Because the compiler will always help us generate a default, and set the field to 0,false or null;
We want to ensure that the structure values created by the default constructor have logical behavior, so you can declare a non-default constructor for initialization (although the compiler throws a default constructor)
At this time Attention! All fields must be initialized in the constructor that you write yourself! Otherwise, the compilation cannot be done.
② can be used when declaring an instance field in a Class! When Initialize it, but this is not allowed in the structure.
The structure must be declared first! Then let the compiler initialize by default or all of itself.
When you copy a struct variable, each field of the structure variable to the left of the = operator is copied directly from the corresponding field of the structure variable on the right.
Assigns a struct variable to another structure variable, provided that the struct variable on the right is fully initialized (that is, all fields have been initialized)
If you do not write your own initialization, the compiler will initialize 0, false, and null by default.
--It is important to note the following!!!!!!!!!!!!!!!!!!!!!!!!!
Very IMPORTANT: When you copy a variable of a value type, you get two copies of the value. Conversely, when you copy a variable of a reference type, you get two references to the same object.
Person Ming = new person ();
Person Gang = new person ();
Ming.age = 23;
Gang = ming;//Here is actually let Ming and Gang both refer to the previous Ming corresponding to the heap memory
Gang.age = 25;
Console.WriteLine (ming.age);//output value is 25.
//--------------------------------------------------------
The above point in the structure here also has reference value!
A struct is a value type, and a variable that duplicates a value type creates a copy of all the data in the variable.
However, a class is a reference type, and copying a reference type variable copies a reference to the original variable! When data is sent in a class variable, all references to that variable change.
"Four" uses enumerations and structs to create value types