C # language basics-struct and enumeration types,

Source: Internet
Author: User
Tags switch case

C # language basics-struct and enumeration types,

Struct and enumeration types

I. struct)

The structure type is a type defined by the user. It is composed of other types, it can contain constructors, constants, fields, methods, attributes, indexers, operators, events, and nested value types. The structure is different from the class in several important aspects: the structure is a value type rather than a reference type, and the structure does not support inheritance.

The main idea of structure is to create small objects, such as Point and FileInfo. This saves memory because there is no additional reference as required by the class object. For example, when declaring an array containing thousands of objects, this can cause a huge difference.

A struct is a variable group. A group of variables is actually a custom set that can contain various types of data in the same usage as a set.

1. Definition

Struct is generally defined on the Main function, which is located under the Class and serves as a Class. In general, Struct is defined before the Main function and can be used in all aspects of the Main function, public is a public variable.

Format:

Struct + struct name

{

Public int + variable name;

Public string + variable name;

Public int + variable name;

}

The above is the format of defining a struct, which contains many data types, such as integer int, string, decimal;

If the struct is defined in the Main function and can be used in the Main function, it is generally defined in front of the Main function, and can be used in all places in the Main function, public is a public variable.

Format:

Struct student

{

Public int no;

Public string name;

Public int Csharp;

Public int web;

Public int database;

Public int sum;

}

2. Usage

(1) A student type struct is defined outside the Main function and used in the Main function:

(2) assign values to each element. struct name + vertex + variable name = value in struct.

(3) print after assigning values.

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

{

Lch. no = 1;

Lch. name = "Li Changhui ";

Lch. Cshap = 88;

Lch. web = 90;

Lch. database = 98;

Lch. sum = lch. Cshap + lch. web + lch. database;

}

Console. WriteLine (lch. no, lch. name, lch. Cshap, lch. web, lch. database, lch. sum );

[Case 1] defines a structure of the jiegouti type, and the struct variable in the output type. The result displayed after the program runs is as follows:

 

[Technical point] defines a structure of the jiegouti type, which has three Structure Variables: fenshu, name, and kecheng. In the main function, defines a parameter to receive the structure variable, and then outputs the name, returns a string.

3. struct type elements include struct types

// If you want other added classes to use this struct, add public

Public struct student

{

// To allow other classes to access the variables, public must be added.

Public int nianling;

Public string name;

Public string sex;

// The struct can contain another struct.

Public One qq;

// An array can be defined directly, but no space is opened.

Public string [] shuzu;

}

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)

// Initialization is required before use.

Student st = new student ();

// The initialized variable name can be viewed as a Class Object

St. name = "James ";

// The class object name cannot be the same

St. nianling = 21;

St. sex = "male ";

St. name = "Wang Wu ";

// Use the variables listed in the variable name for usage.

Console. WriteLine (st. name );

// Struct contains another struct type. You can click a variable below

St. qq. abc = "qsqs ";

// Open up space before use

St. shuzu = new string [9];

// Array element Assignment Method

St. shuzu [0] = "Zhao liu ";

// You can initialize the class multiple times. Pay attention to different variable names.

Student st1 = new student ();

St1.name = "Li Si ";

St1.nianling = 22;

St1.sex = "female ";

# Endregion

}

[Case 2] The structure element of the jiegouti type can also contain another struct, define another variable public int [] shuzu, and output the newly defined array variable. The running result is as follows:

 

[Technical point] defines the structure of the jiegouti type, the original three Structure Variables, fenshu, name, kecheng, and adds a variable public int [] shuzu, the structure element can also contain a new struct. In the main function, the new initialization defines a parameter to receive the new structure variable, then the name is output, and a string is returned.

[Case 3] The structure element of the jiegouti type can also contain another struct, define another variable public int [] shuzu, and output the newly defined array variable. Multiple output variables can be connected with "+.

 

[Technical point] defines the structure of the jiegouti type, the original three Structure Variables, fenshu, name, kecheng, and adds a variable public int [] shuzu, the structure element can also contain a new struct. In the main function, the new initialization defines a parameter to receive the new structure variable, then the name is output, and a string is returned.

Comprehensive Exercise: use struct to put student IDs, names, and scores into a set, and then extract and print them out.

 

Ii. Enumeration type

Enumeration (enum) is a special form of value type. It inherits from System. Enum and provides an alternative name for the base type value. Enumeration types include names, basic types, and a set of fields. The base type must be a built-in signed (or unsigned) Integer type (such as Byte, Int32, or UInt64) other than the char type ). It can also be said that the enumeration type is a set of constants.

1. Definition: Enum... {E}

The default basic type of enumeration elements is int. By default, the value of the first enumeration number is 0, and the value of each enumeration number increases by 1. For example:

// Enumeration is a set of constants. Generally, the data type is not specified after the colon.

Enum meiju:

{

// You can specify the default index in the enumeration. For example, index 3 does not work in the console;

// Separated by commas

One = 3,

Two = 6,

Three,

// If it is equal to a constant, it is equal to this constant. The comma in the last row can be omitted.

Four = two

}

Static void Main (string [] args)

{

Console. WriteLine (meiju. one );

Console. ReadLine ();

2. Usage

Enumeration can also specify the data type, which is generally not specified.

You can select the base type when defining the enumeration type, but the base types that can be used are limited to long, int, short, and byte. For example:

For example, enum meiju: int

[Case 1] defines an enumeration type and outputs a constant of the enumeration type. The result displayed after the program is run, for example:

 

[Technical point] defines an int Enumeration type, which contains four enumerated constants and defines another method. This method has a parameter used to accept a value of the enumeration type, then a value of the int type is returned.

3. Note: numbers cannot be executed separately in enumeration. The system will produce an error, mainly constraints on strings;

Example: 6;

[Case 2] define an enumeration type with six enumeration types. Figure 2 shows the result after the program runs.

 

[Technical point] Although enumeration is a set of constants, numbers cannot be placed directly in the enumeration type, and the system does not run.

Comprehensive 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.