C # Enumeration type and struct,

Source: Internet
Author: User
Tags switch case

C # Enumeration type and struct,

Note: Both the enumeration type and the schema type belong to the value type.

 

Struct: it is a custom set, which can contain various types of elements. Its usage is basically the same as that of the set.

I. defined methods:

Struct student

{

Public int nianling;

Public int fenshu;

Public string name;

Public string sex;

Public int sum;

}

The preceding statement defines a struct named student, which contains the age, score, sum, and string name and gender of the int type.

Ii. Usage:

A struct named student is defined outside the main function for use in the main function.

Student st = new student (); // This sentence defines a struct of the student type named st in the main function.

Assign values to each element as follows: (struct name + vertex + variable name = value in struct)

Main Function

{

St. nianling = 22;

St. fenshu = 80;

St. name = "Xiao Li ";

}

After a value is assigned, the assigned items can be printed.

3. struct types include struct types:

You can define a struct in the previous student struct.

Public shuxing sx; // represents a shuxing struct variable group.
}
Public struct shuxing
{
Public double tizhong;
Public double shengao;
Public int nianling;
Public string hunfou;
}

In this way, you can save the re-initialization structure when using it.

 

Course content:
Public struct student // if you want other added classes to use this struct, add public
{
Public int nianling; // to allow other classes to access the variables, public must be added.
Public string name;
Public string sex;
Public One qq; // The struct can contain another struct.
Public string [] shuzu; // you can directly define an array, but it does not open up space.
}
Public struct One
{
Public string nb;
Public string abc;
}
Static void Main (string [] args)
{

# Region
// Assign values to each element: (struct name + vertex + variable name = value in struct)
Student st = new student (); // Initialization is required before use
St. name = "Zhang San"; // The initialized variable name can be viewed as a class object.
St. nianling = 21; // The class object name cannot be the same
St. sex = "male ";
St. name = "Wang Wu ";
// Use the variables listed in the variable name for usage.
Console. WriteLine (st. name );
St. qq. abc = "qsqs"; // The struct contains another struct type. You can click a variable below
St. shuzu = new string [9]; // you need to open up space before using it.
St. shuzu [0] = "Zhao "; // array element Assignment Method

Student st1 = new student (); // You can initialize the class multiple times. Pay attention to different variable names.
St1.name = "Li Si ";
St1.nianling = 22;
St1.sex = "female ";
# Endregion
}

 

 

 


Enumeration type:
1. The Enumeration type is only applicable to strings. It is meaningless for indexes.
2. Set of constants. These constants can only be values and cannot be assigned values.
3. Use constants to represent the referenced string. This saves the trouble of repeatedly writing long strings.

Exercise:
20 people vote, five candidates, switch case

// Switch case enumeration for 20 votes
// Enter 1, 2, 3, 4, 5 when voting
// Use 12345 to determine which candidate gets the ticket
// Calculated number of votes
// The highest winner
Console. WriteLine ("Vote for shift leader! 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 ++)
{
Console. Write ("please vote" + I + :");
Shuzu [I-1] = int. Parse (Console. ReadLine ());
}
Console. WriteLine ("the vote is over! Press enter to start counting the number of votes! ");
Console. ReadLine ();
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) 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 ("James wins! Number of votes: "+ zhangsan );
}
Else if (lisi> zhangsan & lisi> wangwu & lisi> zhaoliu & lisi> fengqi)
{
Console. WriteLine ("Li Si wins! Number of votes: "+ lisi );
}
Else if (wangwu> lisi & wangwu> zhangsan & wangwu> zhaoliu & wangwu> fengqi)
{
Console. WriteLine ("Wang Wu wins! Number of votes: "+ wangwu );
}
Else if (zhaoliu> lisi & zhaoliu> wangwu & zhaoliu> zhangsan & zhaoliu> fengqi)
{
Console. WriteLine ("Zhao six wins! Number of votes: "+ zhaoliu );
}
Else if (fengqi> lisi & fengqi> wangwu & fengqi> zhaoliu & fengqi> zhangsan)
{
Console. WriteLine ("Feng Qi wins! Number of votes: "+ fengqi );
}
Console. WriteLine ("invalid ticket count:" + zuofei );
Console. ReadLine ();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.