struct, enum type

Source: Internet
Author: User
Tags switch case

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 Tizhong;
public double Shengao;
public int nianling;
public string Hunfou;
}

This will save you the ability to initialize the structure again when you use it.

Public struct student//If you want the struct to be used by other classes that you add, you need to precede the public
{
public int nianling;//variables that you want other classes to have access to are added with public
public string name;
public string sex;
Public one qq;//can contain another structure body
Public string[] shuzu;//can define an array directly, but there is no space to open
}
public struct One
{
public string NB;
public string ABC;
}
static void Main (string[] args)
{

#region
Assign values to each element inside: (struct name + dot + variable name = value inside struct)
Student st = new Student ();//need to initialize first before using
St.name = "Zhang San";//the initialized variable name can be considered as a class object
St.nianling = 21;//Class object name is not the same
St.sex = "male";
St.name = "Harry";
Use variable name points to make use of variables.
Console.WriteLine (St.name);
St.qq.abc= "QSQS";//structure contains another structure type, can directly point out the following variables
St.shuzu = new string [9];//need to open space before use
St.shuzu[0] = "Zhao Liu";//Array element assignment method

Student st1 = new Student ();//You can initialize the class multiple times, note the different variable names
St1.name = "John Doe";
st1.nianling = 22;
St1.sex = "female";
#endregion
}

Enum Type:
1. Enumeration type 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

Note: Both enum types and struct bodies are value types.

Practice:
20 votes, five candidates, with a switch case

20 people voted switch case enumeration
When you vote, enter 1,2,3,4,5.
Use 12345来 to determine which candidate got the ticket.
Number of votes to be calculated
Win the highest ticket

Console.WriteLine ("Please enter 1,2,3,4,5 to represent Zhang San, John Doe, Harry, Zhao Liu, Feng Qi, respectively)";
int a=0;int b=0;int C=0;int d=0;int e=0;int f = 0;
for (int i = 1; i < 21;i++)
{
Console.Write ("+i+" of the student's Ballot: ");
int n = Int. Parse (Console.ReadLine ());
Switch (n)
{
case (int) H.one:



A+=1;
Break
case (int) H.two:

B+=1;
Break
case (int) H.three:

C+=1;
Break
case (int) H.four:

Break
D+=1;
case (int) H.five:

E+=1;
Break
Default

f + = 1;
Break

}
}
if (a>b&&a>c&&a>d&&a>e)
{
Console.WriteLine ("Zhang San elected! The number of votes is: "+a);
}
else if (b > a && b > C && B > D && B > E)
{
Console.WriteLine ("John Doe elected! The number of votes is: "+ b);
}
else if (C > B && C > a && C > D && C > E)
{
Console.WriteLine ("Harry elected! The number of votes is: "+ C);
}
else if (d > b && d > C && d > a && d > E)
{
Console.WriteLine ("Zhao Liu elected! The number of votes is: "+ D);
}
else if (E > B && e > C && e > D && e > a)
{
Console.WriteLine ("Von Seven elected! The number of votes is: "+ e);
}
Console.WriteLine ("The number of votes abstained is:" +f);
Console.ReadLine ();

struct, enum type

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.