structure :
1, Definition: Package small related variable group, which can put a series of variables;
is a variable group, put a set of variables together, the structure is generally defined on the main function, under Class, as a class; The general struct is defined in front of the main function, where the main function can be used, and the arguments preceded by the public to represent the common variables.
Format:
public struct Jiegouti
Cases:
public struct Jiegouti
{
public int fenshu;//defining a set of variables
public string name;
public string kecheng;
public int[]shuzu;
public shuxing sx;//represents a shuxing (equivalent to an array) of struct-body variable groups;
}
Define a shuxing structure body
public struct shuxing
{
public int tizhong;
public string Hunfou;
public int nianling;
}
2. Usage:
Outside of the main function, we define a struct with the name Jiegouti type, which is used in the main function:
static void main (string[] args)
{
Jiegouti jgt = new jiegouti (); //initializes the struct, which defines a struct of type Jiegouti named JGT in the main function.
here is the value assigned to each element inside: struct name +.+ Variable name = value in struct;
Jiegouti JGT = new Jiegouti ();
Jgt.fenshu = 95;//by initializing the name of the struct, the variable name is assigned to the value of the value used.
Jgt.name = "Xiao Li";
Jgt.kecheng = "Language";
Jgt.shuzu=new Int[9];
Jgt.shuzu[0]=1;
When the assignment is complete, print:
Console.WriteLine (Jgt.shuzu[0]);
3, The structure can also be re-created in the structure, each initialization, is a new structure, which also contains those variables, but also can be re-assigned value,
Jiegouti JGT2 = new Jiegouti ();
Jgt2.name = "Xiao Wang";
Console.WriteLine (Jgt.name+jgt2.name);
Console.ReadLine ();
4. Struct type element contains struct type
After this type of Jiegouti, a shuxing is defined;
public struct Jiegou
{
public string Banji;
public string xingming;
public int nianling;
public double Fenshu;
public string Kecheng;
Public shuxing SX;//This declares a shuxing type of sx,shuxing is another type of struct that we define
}
Define a shuxing structure body
public struct shuxing
{
public int tizhong;
public string Hunfou;
public int nianling;
Here, when assigning values, pay attention to its format:variable name = value inside the JGT+.+SX+.+SX;
10-23c# Foundation--Structural body