C # indexer
Index or class structure allows for example according to the index value in the same manner as an array, indexed with similar properties, except that the indexer is accessed with reference to.
Indexer and array comparison:
(1) an index value of index (Index) type is not limited
(2) index allows overloading
(3) the index is not a variable
Index different properties and
(1) to identify by name attribute, the index identifies a functional form
(2) index may be overloaded, the properties may not
(3) the index can not be declared as static, you can attribute
A simple example indexer
the System the using; the using the System.Collections; public class IndexerClass { Private String [] = new new name String [2]; // index defined in this keyword must, in fact, this is after the object class instantiation public string this [int index] { // get method implemented indexer get { IF (index <2) { return name [index]; } return null; } // set method implemented indexer set { IF (index <2) { name [ index] = value; } } }
} the Test class public { static void Main () { // indexer uses IndexerClass Indexer = new new IndexerClass (); // "=" the right number of indexers assignment, in fact, call its set method Indexer [0] = "Joe Smith "; the indexer [. 1] =" John Doe "; value // output indexer, in fact, it calls the get method Console.WriteLine (the indexer [0]); Console.WriteLine (the indexer [. 1]); } }
String as a subscript, for accessing the indexer
IndexerClass class public { // string used as an internal standard when the indexer, use the Hashtable Private the Hashtable = new new name the Hashtable (); // index defined in this keyword must, in fact, this is after the object class instantiation public string the this [String index] { GET {return name [index] .ToString (); SET {Name.Add (index, value);} } } public class the Test { static void the Main () { IndexerClass the Indexer new new IndexerClass = (); Indexer [ "A0001"] = "John Doe"; the Indexer [ "A0002"] = "John Doe"; Console.WriteLine (the Indexer [ "A0001"]); Console.WriteLine (the Indexer [ "A0002"]); } }
Indexer overload
IndexerClass class public { Private the Hashtable = new new name the Hashtable (); //. 1: access by key Values public String the this [int index] { GET {return name [index] .ToString ();} SET {Name.Add (index , value);} } // 2: access by Key Values public int the this [String aName] { GET { // is actually stored in the Hashtable DictionaryEntry (dictionary) type, if a to traverse the Hashtable, it is necessary to use DictionaryEntry the foreach (D in the DictionaryEntry name) { IF (d.Value.ToString () == aName) { return Convert.ToInt32 (d.Key); } } -1 return; } SET { Name.Add (value, aName); } } } public class the Test { static void the Main () { IndexerClass the Indexer new new IndexerClass = (); // use the first indexer Indexer [1] = "John Doe"; // set using the accessor Indexer [2] = "John Doe"; Console.WriteLine ( "name No. 1:" + Indexer [1]) ; // get accessor using Console .WriteLine ( "No. 2 name:" + indexer [2]); Console.WriteLine (); // use the second indexer the Indexer [" Wang Wu "] = 3;Use // set accessor Console.WriteLine ( "Joe Smith's number is:" + indexer [ "Joe Smith "]); // get accessor of use Console.WriteLine ( "John Doe number is:" + Indexer [ "John Doe"]); Console.WriteLine ( "Wang Wu numbering is:" + Indexer [ "Wang Wu"]); } }
Multi-parameter indexer
using System; using System.Collections; //入职信息类 public class EntrantInfo { //姓名、编号、部门 private string name; private int number; private string department; public EntrantInfo() { } public EntrantInfo(string name, int num, string department) { this.name = name; this.number = num; this.department = department; } public string Name { get { return name; } set { name = value; } } public int Num { Number {return GET;} SET = {Number value;} } public String Department { GET Department {return;} SET = {Department value;} } } // declare a class EntrantInfo indexer public class IndexerForEntrantInfo { Private the ArrayList ArrLst; // for storing EntrantInfo class public IndexerForEntrantInfo () { ArrLst new new = the ArrayList (); } // declare an indexer: name to find and access sector number information public String the this [String name, int NUM] { GET { the foreach (EN EntrantInfo in ArrLst) { IF (en.Name == == name && en.Num NUM) { return en.Department; } } ; return null } SET { when a predetermined C #, examples of a class constructor or by calling class: // new keywords you must use the new key ArrLst.Add (new EntrantInfo (name, NUM, value)); } } // declare an indexer: to find names and department numbers public ArrayList the this [int NUM] { GET { ArrayList the TEMP = new ArrayList (); the foreach (EN EntrantInfo in ArrLst) { IF (en.Num == NUM) { Console.WriteLine(en.Name); temp.Add ( EN); } } Return TEMP; } } // declare multiple versions can also indexer ... } public class the Test { static void the Main () { IndexerForEntrantInfo = new new IndexerForEntrantInfo Info (); // the this [String name, int NUM] of use Info [ "Joe Smith", 101] = "personnel Department"; Info [ "John Doe", 102] = "administrative unit"; Console.WriteLine (Info [ "Joe Smith", 101]); Console.WriteLine ( info [ "John Doe", 102]); Console.WriteLine (); // the this [int NUM] using the foreach (EN EntrantInfo info in [102]) { Console.WriteLine (en.Department); } } }
Index or class structure allows for example according to the index value in the same manner as an array, indexed with similar properties, except that the indexer is accessed with reference to.
Indexer and array comparison:
(1) an index value of index (Index) type is not limited
(2) index allows overloading
(3) the index is not a variable
Index different properties and
(1) to identify by name attribute, the index identifies a functional form
(2) index may be overloaded, the properties may not
(3) the index can not be declared as static, you can attribute
A simple example indexer
the System the using; the using the System.Collections; public class IndexerClass { Private String [] = new new name String [2]; // index defined in this keyword must, in fact, this is after the object class instantiation public string this [int index] { // get method implemented indexer get { IF (index <2) { return name [index]; } return null; } // set method implemented indexer set { IF (index <2) { name [ index] = value; } } }
} the Test class public { static void Main () { // indexer uses IndexerClass Indexer = new new IndexerClass (); // "=" the right number of indexers assignment, in fact, call its set method Indexer [0] = "Joe Smith "; the indexer [. 1] =" John Doe "; value // output indexer, in fact, it calls the get method Console.WriteLine (the indexer [0]); Console.WriteLine (the indexer [. 1]); } }
String as a subscript, for accessing the indexer
IndexerClass class public { // string used as an internal standard when the indexer, use the Hashtable Private the Hashtable = new new name the Hashtable (); // index defined in this keyword must, in fact, this is after the object class instantiation public string the this [String index] { GET {return name [index] .ToString (); SET {Name.Add (index, value);} } } public class the Test { static void the Main () { IndexerClass the Indexer new new IndexerClass = (); Indexer [ "A0001"] = "John Doe"; the Indexer [ "A0002"] = "John Doe"; Console.WriteLine (the Indexer [ "A0001"]); Console.WriteLine (the Indexer [ "A0002"]); } }
Indexer overload
IndexerClass class public IndexerClass class public { { Private the Hashtable = new new name the Hashtable (); Private the Hashtable = new new name the Hashtable (); //. 1: access by key Values public String the this [int index] { GET {return name [index] .ToString ();} SET {Name.Add (index , value);} } // 2: access by Key Values public int the this [String aName] { GET { // is actually stored in the Hashtable DictionaryEntry (dictionary) type, if a to traverse the Hashtable, it is necessary to use DictionaryEntry the foreach (D in the DictionaryEntry name) { IF (d.Value.ToString () == aName) { return Convert.ToInt32 (d.Key); //. 1: access by key Values public String the this [int index] { GET {return name [index] .ToString ();} SET {Name.Add (index , value);} } // 2: access by Key Values public int the this [String aName] { GET { // is actually stored in the Hashtable DictionaryEntry (dictionary) type, if a to traverse the Hashtable, it is necessary to use DictionaryEntry the foreach (D in the DictionaryEntry name) { IF (d.Value.ToString () == aName) { } } Return -1; } SET { Name.Add (value, aName); } } } public class the Test { static void the Main () { IndexerClass the Indexer new new IndexerClass = (); // use the first indexer the Indexer [ 1] = "John Doe"; // set using the accessor Indexer [2] = "John Doe"; Console.WriteLine ( "name No. 1:" + Indexer [1]) ; // get accessor use Console.WriteLine ( "No. 2 name:" + indexer [2]); Console.WriteLine (); // use the second indexer Console.WriteLine ( "Joe Smith's number is:" + indexer [ "Joe Smith "]); // get accessor of use Console.WriteLine ( "John Doe number is:" + Indexer [ "John Doe"]); the Indexer [" Wang Wu "] = 3;Use // set accessor Console.WriteLine ( "Wang Wu numbering is:" + Indexer [ "Wang Wu"]); } }
Multi-parameter indexer
using System; using System.Collections; //入职信息类 public class EntrantInfo { //姓名、编号、部门 private string name; private int number; private string department; public EntrantInfo() { } public EntrantInfo(string name, int num, string department) { this.name = name; this.number = num; this.department = department; } public string Name { get { return name; } set { name = value; } } public int Num { Number {return GET;} SET = {Number value;} } public String Department { GET Department {return;} SET = {Department value;} } } // declare a class EntrantInfo indexer public class IndexerForEntrantInfo { Private the ArrayList ArrLst; // for storing EntrantInfo class public IndexerForEntrantInfo () { ArrLst new new = the ArrayList (); } // declare an indexer: name to find and access sector number information public String the this [String name, int NUM] { GET { the foreach (EN EntrantInfo in ArrLst) { if (en.Name == name && en.Num == num) { Return en.Department; } } return null; } SET { // new keywords: C # predetermined, when a class instance or class constructor call must use the new key ArrLst.Add (new EntrantInfo (name, num , value)); } } // declare an indexer: to find names and department numbers public ArrayList the this [int NUM] { GET { ArrayList the TEMP = new new ArrayList (); foreach (EntrantInfo EN in ArrLst) { temp.Add ( EN); } IF (en.Num == NUM) { } Return TEMP; } } // declare multiple versions can also indexer ... } public class the Test { static void the Main () { IndexerForEntrantInfo = new new IndexerForEntrantInfo Info (); // the this [String name, int NUM] using Info [ "Joe Smith", 101] = "personnel Department"; Info [ "John Doe", 102] = "administrative unit"; Console.WriteLine (Info [ "Joe Smith", 101]); Console.WriteLine (Info [ "John Doe", 102]); Console.WriteLine (); // the this [int NUM] using the foreach (EN EntrantInfo Info in [102]) { Console.WriteLine(en.Name); Console.WriteLine (en.Department); } } }