The variables defined by int can only be used for shaping data, and the variables defined by string can only put string data, which are built-in data types;
Struct{},class (classes) are user-defined data types that can put arbitrary types of data.
Defining a variable with a data type is like creating a variable with a template specification so that it cannot store data arbitrarily, such as a variable of type int cannot store a string type of data;
All data types (int,string,struct out of student) are like all kinds of templates, and the struct-student is the data type template we create ourselves.
Class templates can write functions, so more advanced than struct templates, class-defined variables we call objects.
Object-oriented is a kind of thought, a kind of thinking.
In object-oriented programming, a class is the smallest unit and is the interaction between classes and classes.
The type of data created by class as a template is called an object.
The scope of the variable: public (available everywhere in the project), private can only be used within the class.
You cannot create an array (struct) with an object created by class, and you can create a collection (array).
Each time you create an object you need to open up space with new. Structs can automatically open up space.
object is the base class for all data types: like creatures for all animals, plants, microorganisms and so on.
Small knowledge Point: Scaling code: #region endregion
Blue BLOCK: Member variable
Purple Block: Member method
When programming, right-click function name, go to Definition
1. Example: through a struct array storage, processing student information, according to the following requirements from the console cycle input 10 students of the information and scores (student number, name, C language score) using the structure, the control desk to print the total score and the average score, and print the highest score of the class, the lowest score of the student information, According to the high and low print the class score book and discharge position, require the use of structure, custom functions, arrays to complete the subject.
#region= = Student Course title = =structStudent { Public stringCode; Public stringname; Public decimaldegree; } Static voidMain (string[] args) {Console.Write ("Please enter the number of people:"); intn =int. Parse (Console.ReadLine (). ToString ()); decimalsumfen=0, avg=0, max=0, min=0; Student[] Sumstu=NewStudent[n]; //cyclic entry According to the number of people entered for(inti =0; I < n; i++) {Console.WriteLine ("Please enter section"+ (i+1)+"A student's information"); Console.WriteLine ("study number name C language score, separated by tab key"); strings =Console.ReadLine (); string[] Sarr = S.split (New Char[]{'\ t'});//use split to separate by delimiterSumstu[i].code= sarr[0]; Sumstu[i].name= sarr[1]; Sumstu[i].degree=decimal. Parse (sarr[2]); Sumfen+=Sumstu[i].degree; } Console.WriteLine ("****************************************"); Console.WriteLine ("Total Score:"+Sumfen. ToString ()); Avg= Sumfen/N; Console.WriteLine ("Average score:"+avg. ToString ()); //Bubble Sort for(inti =0; I < n1; i++) { for(intj = i+1; J < N; J + +) { if(Sumstu[i].degree <Sumstu[j].degree) {Student Zhong=NewStudent (); Zhong=Sumstu[i]; Sumstu[i]=Sumstu[j]; SUMSTU[J]=Zhong; }}} Console.WriteLine ("Highest score:"+sumstu[0].degree. ToString ()); Console.WriteLine ("Minimum Score:"+sumstu[n-1].degree. ToString ()); Console.WriteLine ("the results of this class are listed below:"); Console.WriteLine ("****************************************"); Console.WriteLine ("Rank Study number name result"); for(inti =1; I <= N; i++) {Console.WriteLine (i+"\ t"+sumstu[i-1].code+"\ t"+sumstu[i-1].name+"\ t"+sumstu[i-1].degree); } console.readline (); }
2. Working with student information through the array collection
classProgram {//use an array to store 10 student information, (instead of putting the structure in an array, just practice the use of the array)//Define student information structure Body Public structStudent { Public stringSno; Public stringname; Public intdegree; } Static voidMain (string[] args) { //defines an array of student element types, storing 10 student informationArray Myarr = array.createinstance (typeof(Student),Ten); for(inti =0; i < myarr.length; i++) {Student AAA; Console.WriteLine ("Please enter section"+ (i +1) +"Student's school number, name, score"); Aaa.sno=Console.ReadLine (); Aaa.name=Console.ReadLine (); Aaa.degree=Convert.ToInt32 (Console.ReadLine ()); Myarr.setvalue (AAA, I); } //Split LineConsole.WriteLine ("********************************************************"); //To find the total score and the average intsum =0; for(inti =0; i < myarr.length; i++) { Objectaaa//defines an object to receive an element taken from an arrayAAA =Myarr.getvalue (i); Student b= (Student) AAA;//force the removed element to be convertedSum + =B.degree; } Console.WriteLine ("Total score is"+ Sum +"evenly divided into"+ Sum/myarr.length); Console.WriteLine ("********************************************************"); //highest score, lowest score by bubble sort int[] A =New int[Ten];//use an array to store the fraction of an element for(inti =1; I <= myarr.length; i++)//bubble sort, sorts the elements in an array by the size of fractions { for(intj =1; J <= Myarr.length-i; J + +) { ObjectAAA, BBB;//defines two objects to receive elements taken from an arrayAAA = Myarr.getvalue (J-1); BBB=Myarr.getvalue (j); Student AA= (Student) AAA;//force the removed element to be convertedStudent BB =(Student) BBB; A[j-1] = Aa.degree;//determines whether the position of an element is swapped by comparing the size of the fraction of the adjacent two elementsA[J] =Bb.degree; if(A[j] > A[j-1]) {Myarr.setvalue (AA, J); Myarr.setvalue (BB, J-1); } } } ObjectZG, ZD; ZG= Myarr.getvalue (0); Student mm=(Student) ZG; ZD= Myarr.getvalue (Myarr.length-1); Student N=(Student) ZD; Console.WriteLine ("maximum is divided into"+ Mm.degree +"name"+ Mm.name +"School Number"+ Mm.sno +"minimum is divided into"+ N.degree +"name"+ N.name +"School Number"+N.sno); //Split LineConsole.WriteLine ("********************************************************"); //Print a score sheet for(inti =0; i < myarr.length; i++) { ObjectAAA; AAA=Myarr.getvalue (i); Student b=(Student) AAA; Console.WriteLine ("name"+ B.name +"School Number"+ B.sno +"score of"+b.degree); } } }
C # Object-oriented preliminary