Indexer Example
usingSystem;usingSystem.Collections; Public classindexerclass{Private string[] name =New string[2]; //the indexer must be defined with the This keyword, in fact this is the object after the class instantiation Public string This[intIndex] { //implement the Get method of the indexer Get { if(Index <2) { returnName[index]; } return NULL; } //implementing the Set method of the indexer Set { if(Index <2) {Name[index]=value; } } }} Public classtest{Static voidMain () {//use of indexersIndexerclass Indexer =NewIndexerclass (); //The index is assigned to the right of the "=" number, which is actually called its set methodindexer[0] ="Zhang San"; indexer[1] ="John Doe"; //the value of the output indexer is actually called its Get methodConsole.WriteLine (indexer[0]); Console.WriteLine (indexer[1]); }}
The indexer is accessed using a string as the subscript
Public classindexerclass{//when using string as the indexer subscript, use Hashtable PrivateHashtable name =NewHashtable (); //the indexer must be defined with the This keyword, in fact this is the object after the class instantiation Public string This[stringIndex] { Get{returnName[index]. ToString (); Set{name. ADD (index, value); } }} Public classtest{Static voidMain () {Indexerclass Indexer=NewIndexerclass (); indexer["A0001"] ="Zhang San"; indexer["A0002"] ="John Doe"; Console.WriteLine (indexer["A0001"]); Console.WriteLine (indexer["A0002"]); }}
Overloading of indexers
Public classindexerclass{PrivateHashtable name =NewHashtable (); //1: Access values via key Public string This[intIndex] { Get{returnName[index]. ToString (); } Set{name. ADD (index, value); } } //2: Access key via Values Public int This[stringAName] { Get { //Hashtable is actually stored in the DictionaryEntry (dictionary) type, if you want to traverse a hashtable, you need to use the DictionaryEntry foreach(DictionaryEntry Dinchname) { if(d.value.tostring () = =aName) { returnConvert.ToInt32 (D.key); } } return-1; } Set{name. ADD (value, aName); } }} Public classtest{Static voidMain () {Indexerclass Indexer=NewIndexerclass (); //use of the first type of indexerindexer[1] ="Zhang San";//use of the set accessorindexer[2] ="John Doe"; Console.WriteLine ("the name is numbered 1:"+ indexer[1]);//use of Get accessorsConsole.WriteLine ("the name is numbered 2:"+ indexer[2]); Console.WriteLine (); //use of the second type of indexerConsole.WriteLine ("the number of the Zhang San is:"+ indexer["Zhang San"]);//use of Get accessorsConsole.WriteLine ("the number of the John Doe is:"+ indexer["John Doe"]); indexer["Harry"] =3;//use of the set accessorConsole.WriteLine ("the number of the Harry is:"+ indexer["Harry"]); }}
Multi-parameter Indexer
usingSystem;usingSystem.Collections;//onboarding Information Category Public classentrantinfo{//name, number, Department Private stringname; Private intNumber ; Private stringDepartment; PublicEntrantinfo () {} PublicEntrantinfo (stringNameintNumstringDepartment) { This. Name =name; This. Number =num; This. Department =Department; } Public stringName {Get{returnname;} Set{name =value;} } Public intNum {Get{returnNumber ;} Set{Number =value;} } Public stringDepartment {Get{returnDepartment;} Set{Department =value;} }}//to declare an indexer of a class Entrantinfo Public classindexerforentrantinfo{PrivateArrayList Arrlst;//for storing Entrantinfo class PublicIndexerforentrantinfo () {Arrlst=NewArrayList (); } //Declare an indexer: Find and Access department information by name and number Public string This[stringNameintNum] { Get { foreach(Entrantinfo enincharrlst) { if(En. Name = = Name && en. Num = =num) { returnen. Department; } } return NULL; } Set { //New keyword: C # Specifies that when you instantiate a class or call a class's constructor, you must use the new keyArrlst.add (Newentrantinfo (name, num, value)); } } //Declare an indexer: Find names and departments with numbers PublicArrayList This[intNum] { Get{ArrayList temp=NewArrayList (); foreach(Entrantinfo enincharrlst) { if(En. Num = =num) {Temp. Add (en); } } returntemp; } } //You can also declare multiple versions of indexers ...} Public classtest{Static voidMain () {indexerforentrantinfo Info=NewIndexerforentrantinfo (); //use of this[string name, int num]info["Zhang San",101] ="Personnel"; info["John Doe",102] ="Administration Department"; Console.WriteLine (info["Zhang San",101]); Console.WriteLine (info["John Doe",102]); Console.WriteLine (); //use of This[int num] foreach(Entrantinfo eninchinfo[102]) {Console.WriteLine (en). Name); Console.WriteLine (en. Department); } }}
C # Indexer