12, C # Basic finishing (structure)

Source: Internet
Author: User

Structural body

1. Concept:

A struct is a data structure written outside of the main function, composed of different types of data, which are interconnected by data in a single whole.

2. Declaration method:

struct Structure Body name

{

Member variables (consisting of type name + member name)

}

Cases:

 Public struct student//publicis a modifier that can be added without the scope of the entire namespace {publicint Code ; // define variables, each variable is called a property of a struct  Public string Name;  Public string Sex;  Public int Age ;  Public decimal Height;}

3. Call Method:

(1) Initialize the struct (new one)

(2) Assigning values to variables in a struct

Such as:

// continue to use the definition of the structure above New  101"Zhangsan" "Nan" = 173; 

4, using the structure of the code to optimize the processing

----Bubble Sort----

Title: Enter the number of students, enter the name, height, age, average age, and then according to the height of ascending discharge

Idea: Create a structure with name, height, and age parameters, and then set up a set that puts the structure type of each initialization into the collection with three of data through a for loop.

Answer:

Console.WriteLine ("Enter the number of students:");intn =int. Parse (Console.ReadLine ()); ArrayList ar=NewArrayList ();//set up collection fill dataintsum =0; for(inti =0; I < n; i++) {Student SS=Newstudent (); Console.Write ("Please enter your name:"); Ss. Name=Console.ReadLine (); Console.Write ("Please enter your age:"); Ss. Age=int.    Parse (Console.ReadLine ()); Console.Write ("Please enter your height:"); Ss. Height=int. Parse (Console.ReadLine ().    Trim ()); Ar. ADD (ss);//Add a student type of data to the collectionsum = Sum+ss. Age;//Calculate Total Score} for(inti =0; I < n; i++){     for(intj = i; J < N; J + +)    {        //Establish intermediate values, convert ar[i], ar[j] to the type of student, and then judge the heightStudent S1 =(student) ar[i]; Student S2=(student) ar[j]; if(S1. height<S2. Height) {Ar[i]=S2; AR[J]=S1; }    }}foreach(Student Ainchar) {Console.Write ("Name:"+a.name); Console.Write ("Height:"+a.height); Console.Write ("Age:"+a.age); Console.Write ("\ n");}

12, C # Basic finishing (structure)

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.