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)