Notice:Both enum types and structures belong to value types.
Structure:It is a custom collection that can place various types of elements in it, and the usage is roughly the same as the collection.
1. Definition method:
struct student { public int nianling; public int fenshu; public string name; public string sex; public int sum; }
The above statement defines a structure named student, which contains the age, score, sum of type int, and name and gender of type string.
2. Usage:
A structure named student is defined outside the main function to facilitate use in the main function.
student st = new student();//This sentence defines a structure of student type named st in the main function.
Below, start assigning values to each element inside: (struct name + point + variable name inside the structure = value)
Under the main function
{ =22; =80; ="Little Li"; }
After the assignment is completed, the assigned item can be printed out.
3. Structural type contains structure type:
You can define a structure in the previous student's structure.
public shuxing sx;//Represents a shuxing structure variable group} public struct shuxing { public double tizhong; public double shengao; public int nianling; public string hunfou; }
This way, you can save the structure of the structure again when using it.
Class content:
public struct student//If you want other added classes to use this structure, you need to add public before it. { public int nianling;//If you want other classes to access variables, you need to add public public string name; public string sex; public One qq;//The structure can contain another structurepublic string[] shuzu;//You can directly define an array, but there is no room for opening up} public struct One { public string nb; public string abc; } static void Main(string[] args) { #region // Assign a value to each element inside: (struct name + point + variable name inside the structure = value)student st = new student();//You need to initialize it before using it = "Zhang San";//The initialized variable name can be regarded as a class object = 21;//The name of the class object cannot be the same = "male"; = "Wang Wu"; //When using the variable name, use the variables to point out the variables for use(); ="qsqs";//The structure contains another structure type, and you can directly point out a variable below one = new string [9];//You need to open up space before using[0] = "Zhao Liu";//Array element assignment method student st1 = new student();//The class can be initialized multiple times, pay attention to different variable names = "Li Si"; = 22; = "female"; #endregion }
Enumeration type:
1. Enumeration type is only for strings, and it has no meaning for indexes.
2. Set of constants, these constants can only take values, but cannot be assigned values.
3. Use constants to represent the referenced string, which can eliminate repeated writing of long strings.
practise:
20 votes, five candidates, switch case
//20 people vote switch case enum//Enter 1, 2, 3, 4, 5 when voting //Use 12345 to determine which candidate wins the vote//Calculate the number of votes//The winner with the highest vote("Vote to select the squad leader! Please enter 1, 2, 3, 4, and 5 to represent Zhang San, Li Si, Wang Wu, Zhao Liu, and Feng Qi respectively"); int[] shuzu = new int[20]; for (int i = 1; i <= 20; i++) { ("Please leave" + i + "A classmate comes to vote:"); shuzu[i - 1] = (()); } ("Voting is over! Press Enter to start counting the number of votes!"); (); int zhangsan = 0, lisi = 0, wangwu = 0, zhaoliu = 0, fengqi = 0, zuofei = 0; for (int i = 0; i < 20; i++) { switch (shuzu[i]) { case (int): zhangsan++; break; case (int): lisi++; break; case (int): wangwu++; break; case (int): zhaoliu++; break; case (int): fengqi++; break; default: zuofei++; break; } } if (zhangsan > lisi && zhangsan > wangwu && zhangsan > zhaoliu && zhangsan > fengqi) { ("Zhang San wins! The votes are" + zhangsan); } else if (lisi > zhangsan && lisi > wangwu && lisi > zhaoliu && lisi > fengqi) { ("Li Si won! The votes are" + lisi); } else if (wangwu > lisi && wangwu > zhangsan && wangwu > zhaoliu && wangwu > fengqi) { ("Wang Wu won! The votes are" + wangwu); } else if (zhaoliu > lisi && zhaoliu > wangwu && zhaoliu > zhangsan && zhaoliu > fengqi) { ("Zhao Liu won! The votes are" + zhaoliu); } else if (fengqi > lisi && fengqi > wangwu && fengqi > zhaoliu && fengqi > zhangsan) { ("Feng Qi won! The votes are" + fengqi); } ("The number of votes invalid is:" + zuofei); ();
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.