usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceThe inner class uses the interface to implement the sort {classPerson {Private string_name; Private int_age; PublicPerson (stringNameintAge ) {_name=name; _age=Age ; } Public stringName {Get{return_name;} } Public intAge {Get{return_age;} } Private StaticAgecomparer _agecom =NULL;//is only a static variable. Public StaticIcomparer<person>agecom {Get { if(_agecom = =NULL)//when a static property is accessed for the first time, an object is created automatically. {_agecom=NewAgecomparer (); } return_agecom; } } Private classAgecomparer:icomparer<person> { intIcomparer<person>. Compare (person x, person y) {returnX._age.compareto (y._age); } } } classProgram {Static voidMain (string[] args) {person[] P1=Newperson[5]; p1[0] =NewPerson ("Wang Liang", -); p1[1] =NewPerson ("Zhang Mingmin", +); p1[2] =NewPerson ("Sun Xiaofeng", -); p1[3] =NewPerson ("Hermione", -); p1[4] =NewPerson ("Liu Mingchuan", at); foreach(Person Pinchp1) {Console.WriteLine (P.name+" "+p.age.tostring ()); } Console.WriteLine ("The age is sorted and the results are printed:"); Array.Sort (P1, person.agecom); foreach(Person Pinchp1) {Console.WriteLine (P.name+" "+p.age.tostring ()); } console.readkey (); } }}
C # Inner classes use interface IComparer to implement sorting