Every time I read C #, I saw the index device and it stopped. I didn't know much about it, and I didn't feel any practical,
The so-called ignorance, today I learned a simple Indexer
Main program code:
Static void main (string [] ARGs) {programgroup group = new programgroup (); // instantiate the object // player [] Name = new player [2]; // create an actor array // create an actor object and assign a value to the attribute // name [0] = new player (); // name [0]. name = "mongozidan"; // name [0]. role = "ye Wen"; // create an actor object and assign a value to the attribute. // name [1] = new player (); // name [1]. name = "Xiong Yulin"; // name [1]. role = "Mrs ye"; // group. players = Name; // assign an array to the property console of the object. writeline ("cast:"); // For (INT I = 0; I <group. players. length; I ++) // {// console. writeline (group. players [I]. name); // read the array value through the attribute //} // group [0] = new player (); // group [0]. name = "mongozidan"; // group [0]. role = "ye Wen"; // group [1] = new player (); // group [1]. name = "Xiong Yulin"; // group [1]. role = "Mrs ye"; // For (INT I = 0; I <2; I ++) // {// console. writeline (group [I]. name); //} group. setplayers (); // call the object method to assign the console value to the field. writeline ("played by Dan" + group ["Dan"]. role); console. writeline ("played by Xiong Yulin" + group ["Xiong Yulin"]. role); console. readline ();}
Cast code
Class programgroup // cast class {// Private player [] players; // actor array field // public player [] players // actor array attribute // {// get {return player;} // set {player = value ;} //} private player [] players = new player [2]; // actor array field // public player this [int Index] // indexer, index is an array index // {// get {return players [Index];} // set {players [Index] = value ;} //} public player this [string name] // The indexer {get {int I = 0; For (; I <2; I ++) searched by the actor name) // cyclically search for actors {If (players [I]. name = Name) {break;} if (I = 2) {return NULL; // no blank returned} else {return players [I]; // find the actor element in the returned array} public void setplayers () // assign {players [0] = new player (); players [0] to the array. name = "mongozidan"; players [0]. role = "ye Wen"; players [1] = new player (); players [1]. name = "Xiong Yulin"; players [1]. role = "Mrs ye" ;}} class player // actor class {private string name; // actor name field public string name // actor name attribute {get {return name ;} set {name = value ;}} private string role; // assume the role Field Public String role // assume the role attribute {get {return role ;} set {role = value ;}}}
I feel that the above program is still not quite familiar with the indexer. Let's try again:
Class:
Class squad // class {string name; // class name public string name {get {return name;} set {name = value ;}} int count; // Number of students Public int count {get {return count;} set {COUNT = value ;}}}
Age:
Class Grade // grade class {string name; // grade name public string name {get {return name;} set {name = value ;}} squad [] squads = new squad [2]; // class array public squad this [int Index] {set {squads [Index] = value ;} get {return squads [Index] ;}}
Main Program:
Class program {static void main (string [] ARGs) {Grade = new grade (); // instantiate the object grade. name = "S1"; grade [0] = new squad (); // instantiate the Array Element Object grade [0]. name = "s1t100"; // call the indexer to assign a value to grade [0]. count = 25; grade [1] = new squad (); grade [1]. name = "s1t101"; grade [1]. count = 29; console. writeline (grade. "); console. writeline (grade [0]. name + "contains" + grade [0]. count + "person"); console. writeline (grade [1]. name + "contains" + grade [1]. count + "person"); console. readline ();}}