C # Learning 10: enumeration and struct
An Enumeration type is a set of named constants. Each Enumeration type has a basic type. All Integer types except char can be used as the basic type of an enumeration type.
The structure type is a composite data type that contains constants, fields, methods, attributes, indexers, nested types of operators, struct
Suitable for lightweight objects such as points, rectangles, and colors
Defines an enumeration type and two struct types to implement some basic operations.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace com {Enum season {spring, summer, fall, winter}; struct time {public int hours, minutes, seconds; Public time (int h, int M, int S) {hours = H; minutes = m; seconds = s;} public time (int h, int m) {hours = H; minutes = m; seconds = 0; // each field must be assigned a value, which is different from the class} public int hours () {return hours;} public int minutes () {return minutes;} public int seconds () {return seconds ;}} struct Date {public int year; Public season s; public date (int A, season B) {This. year = A; // This is the keyword, indicating the current instance of the class this. S = B ;}} class program {static void main (string [] ARGs) {season S = season. spring; console. writeline (s); season I = 0; Int J; For (j = 0; j <5; j ++) // The enumerated type starts from 0 {console. writeline (I); I ++;} time t1 = new time (); console. writeline (t1.hours (); // The default value of the struct is 0 time t2 = new time (10, 20, 30); console. writeline (t2.hours (); time t3 = new time (10, 20); console. writeline (t3.hours (); Date = new date (hz0); console. writeline ("Year: {0}, season: {1}", date. year, date. s );}}}