1, it is hereby stated that the following code is copied from the full part of the part, may not be able to directly copy execution.
2, the indexer note 1) If you do not set the array to save, cannot continuously access per2[0],per[1], because such as name is replaced. 2) We create an array to save, we need to create space, we can
String[] Arrname=new string[10]; You can also allocate space here
You can also write a construction method that dynamically allocates space, such as the following code.
3, the indexer in addition to the use of examples, the index number is different, to the object of different fields assigned value.
Public string This[intIndex] { Set { if(Index = =0) {Name="test1"; } Else if(Index = =1) Name="Testother"; ElseName=value; } Get { if(Index = =0) returnName; Else if(Index = =1) returnName +1; Else returnName; } calls//Indexer, cannot access Per2 "0" at once, Per2 "1" Unless return is saved to the arrayPerson Per2 =NewPerson (); per2[0] =" One"; Console.WriteLine (per2[0]); per2[1] =" A"; Console.WriteLine (per2[1]); per2[2] =" -"; Console.WriteLine (per2[2]); Console.WriteLine (per2[0]+" "+per2[1]+" "+per2[2]+" "+per2[3]);
Static voidMain (string[] args) {Person Per2=NewPerson ();p er2[0] =" One"; Console.WriteLine (per2[0]);p er2[1] =" A"; Console.WriteLine (per2[1]);p er2[2] =" -"; Console.WriteLine (per2[2]); Console.WriteLine (per2[0]+" "+per2[1]+" "+per2[2]+" "+per2[3]); Console.readkey ();} Public classperson{intAge ;stringname;//string[] Arrname=new string[10]; You can also allocate space herestring[] arrname;Private BOOLsex; PublicPersonintI//construction method, allocating space for Arrname{arrname=New string[i];} Public string This[intIndex] {Set{if(Index = =0) {arrname[0] ="test1"; }Else if(Index = =1) arrname[1] ="Testother";ElseArrname[index]=value;}Get{if(Index = =0)returnArrname[index];Else if(Index = =1)returnArrname[index];ElsereturnArrname[index] + -;}}
The indexer inside C # note