C # Learning diary 09 --- structure of data type (Struct)
The structure type of the numeric type (struct type ):
After learning the previous simple types, we are already doing some common data operations and text processing. However, when we encounter some complicated data types, for example, in the class management system, you must enter the name, age, phone number, and address of each student. If the data is processed according to the simple data type we have learned earlier, four different variables will be used to store the information of each student input. This means that the workload is too large and it is not intuitive, it is easy to confuse.
The struct defined in C/C ++ is used to put a set of related information together and organize the relevant variables into a single entity. Let's take the above example for example. When I enter a student information, I can apply for a "box" (structure type ), we put his name, age, phone number, address (structure member) in the "box", and then give the box a name, such as the name of "Michael's box ", to view or change Michael's information, you only need to find the member in the box and call it out.
C # fully references the struct type in C/C ++, so its usage and function are the same.
C # define the format of the Struct type:
Access modifier struct type name
{
Access the secondary modification member;
}
The access modifier will be explored later. Briefly speaking about its nature, C # has five access modifiers: public, private, protected, internal, and protected internal. Public is not subject to any restrictions. public is used for common access.
Assign values to members of the structure type, query methods are the same as C/C ++, and structure variable name. member name = value. For example, in the preceding example, three boxes can be assigned. age = 18;
I still use the above example to write a code, input the information of two students, and output it:
Using System; using System. collections. generic; using System. linq; using System. text; namespace Example {class Program {public struct Student {public string name; public uint age; public ulong tel; public string address;} static void Main (string [] args) {Student Firstperson, Secperson; // defines two Student type variables, that is, apply for two 'box' and get the name # region input the first Student information Console. writeLine (first student information); Console. writeLine (input name :); Firstperson. name = Console. readLine (); Console. writeLine (input age :); Firstperson. age = uint. parse (Console. readLine (); // force type conversion converts String type to uint Console. writeLine (enter the phone number :); Firstperson. tel = ulong. parse (Console. readLine (); Console. writeLine (enter the address :); Firstperson. address = Console. readLine (); # endregion # enter the second Student Information Console in region. writeLine (second Student Information); Console. writeLine (input name :); Secperson. name = Console. readLine (); Console. writeLine (input age :); Secperson. age = uint. parse (Console. readLine (); Console. writeLine (enter the phone number :); Secperson. tel = ulong. parse (Console. readLine (); // force type conversion converts String type to ulong type Console. writeLine (enter the address :); Secperson. address = Console. readLine (); # endregion # region outputs the information Console of the two students. writeLine (1. name: {0} age: {1} Tel: {2} address: {3}, Firstperson. name, Firstperson. age, Firstperson. tel, Firstperson. address); Console. writeLine (2. name: {0} age: {1} Tel: {2} address: {3}, Secperson. name, Secperson. age, Secperson. tel, Secperson. address); # endregion }}}
Output result:
When editing the code, I added # region and # enregion to make the pipeline code structure clearer. The function is to contract the compiled code, as shown in the following figure:
The structure type has no restrictions on its member types. The types in the structure can be the same or different, and the number of members in the structure is not limited, for example, we can add a Gender Member char sex for him;
public struct Student { public string name; public uint age; public ulong tel; public string address; public char sex; }
The structure type can even be used as a member of another structure type .. In the above student information example, we change the Member address to a structure type, where the address structure contains nationality, province, city, and street.
Class Program {public struct Student {public string name; public uint age; public ulong tel; public struct address {public string nationality; public string province; // The variable name can be set to public string; public string street;} public char sex; public address Ad; // declare an address type variable. The Ad external function can access members in the address structure by accessing the Ad}
Similarly, if you want to assign values to Members in the address structure
Firstperson. Ad. City = Console. ReadLine (); // Firstperson. Ad. City = Chengdu.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.