10-23c# Foundation--Structural body

Source: Internet
Author: User

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

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.