Note: both enum types and struct bodies are value types .
struct: is a custom collection that can put all kinds of elements in the same way as a collection.
First, the method of definition:
struct student
{
public int nianling;
public int Fenshu;
public string name;
public string sex;
public int sum;
}
The above statement defines a struct, named student, that contains the age, fraction, sum, and string type name, and gender of type int.
Second, usage:
A struct called student is defined outside main main function for use in the main function.
Student st = new Student ();//The phrase is defined in the main function as a struct of type student named St.
The following begins to assign values to each element inside: (struct name + dot + variable name = value inside struct)
Under Main function
{
st.nianling=22;
st.fenshu=80;
St.name= "Xiao Li";
}
The assigned value can be printed after the assignment is complete.
Third, the structure type contains the structure of the body type:
You can define a struct in the previous student structure
Public Shuxing sx;//represents a shuxing struct variable group} public struct shuxing {public double Tizhon G public double Shengao; public int nianling; public string Hunfou; }
This will save you the ability to initialize the structure again when you use it.
Enum Type:
1. Enumeration type is only for strings, no meaning for indexes
2. A collection of constants, which can only be valued and cannot be assigned
3. Use constants to represent referenced strings, which saves you from repeating long strings
Console.WriteLine ("vote for the squad leader! Please enter 1, 2, 3, 4, 5来 respectively represents Zhang San, John Doe, Harry, Zhao Liu, Feng seven"); int[] Shuzu =New int[ -]; for(inti =1; I <= -; i++) {Console.Write ("Please first"+ i +"students to vote:"); Shuzu[i-1] =int. Parse (Console.ReadLine ()); } Console.WriteLine ("The polls are over! Press ENTER to start counting the votes! "); Console.ReadLine (); intZhangsan =0, Lisi =0, Wangwu =0, Zhaoliu =0, Fengqi =0, Zuofei =0; for(inti =0; I < -; i++) { Switch(Shuzu[i]) { Case(int) Houxuanren.one:zhangsan++; Break; Case(int) Houxuanren.two:lisi++; Break; Case(int) Houxuanren.three:wangwu++; Break; Case(int) Houxuanren.four:zhaoliu++; Break; Case(int) Houxuanren.five:fengqi++; Break; default: Zuofei++; Break; } } if(Zhangsan > Lisi && zhangsan > Wangwu && zhangsan > Zhaoliu && zhangsan >Fengqi) {Console.WriteLine ("Zhang San wins! The number of votes is"+Zhangsan); } Else if(Lisi > Zhangsan && Lisi > Wangwu && Lisi > Zhaoliu && lisi >Fengqi) {Console.WriteLine ("John Doe wins! The number of votes is"+Lisi); } Else if(Wangwu > Lisi && wangwu > Zhangsan && wangwu > Zhaoliu && wangwu >Fengqi) {Console.WriteLine ("Harry wins! The number of votes is"+Wangwu); } Else if(Zhaoliu > Lisi && zhaoliu > Wangwu && zhaoliu > Zhangsan && zhaoliu >Fengqi) {Console.WriteLine ("Zhao Liu wins! The number of votes is"+Zhaoliu); } Else if(Fengqi > Lisi && fengqi > Wangwu && fengqi > Zhaoliu && fengqi >Zhangsan) {Console.WriteLine ("Feng Qi wins! The number of votes is"+Fengqi); } Console.WriteLine ("the number of votes voided is:"+Zuofei); Console.ReadLine ();
Public structStudent//If you want the struct to be available to other classes that you add, you need to precede the public { Public intnianling;//variables that you want other classes to have access to are added with public Public stringname; Public stringsex; PublicOne QQ;//can contain another structure body Public string[] Shuzu;//An array can be defined directly, but there is no space to open it } Public structOne { Public stringNB; Public stringABC; } Static voidMain (string[] args) { #region //assign values to each element inside: (struct name + dot + variable name = value inside struct)Student St =NewStudent ();//you need to initialize it before using it .St.name ="Zhang San";//the initialized variable name can be seen as a class objectSt.nianling = +;//The name of the class object cannot be the sameSt.sex ="male"; St.name="Harry"; //Use variable name points to make use of variables.Console.WriteLine (St.name); ST.QQ.ABC="Qsqs";//struct contains another struct type that can be directly point to a variable belowSt.shuzu =New string[9];//need to open space before usest.shuzu[0] ="Zhao Liu";//How array elements are assignedStudent St1=NewStudent ();//classes can be initialized multiple times, paying attention to different variable namesSt1.name ="John Doe"; St1.nianling= A; St1.sex="female"; #endregion }
C # struct-Body enumeration type