Tag: Value person returns the declaration ace using a two-bit array generic class names
Use the index operation [] to access the individual subkeys contained in a standard array.
Definition: The method of accessing a subkey using the index operator [] is called an indexer method
1. Custom Indexer Method (this):
Public classpeoplecollection:ienumerable{PrivateArrayList arpeople=NewArrayList (); //Custom Indexer for class PublicPerson This[intIndex//similar to attribute declarations except for multiple this keywords { Get{return(person) arpeople[index];} Set{Arpeople.insert (index,value);}//using the ArrayList Insert () method }}
2. The generic type directly supports the indexer method:
List<person> mypeople=NewList<person>(); Mypeople.add (Newperson {"Lisa","Simpson", +}); Mypeople.add (Newperson {"Lilei","Simpson", -});//To modify the first value using an indexermypeople[0]=Newperson {"Zhangsan","Simpson", -} ;...
3. Index objects using string values
If you use the generic dictionary<tkey,tvalue> type directly, you can get the indexer method function without having to build it yourself.
Public classpeoplecollection:ienumerable{Privatedictionary<string,person> listpeople=Newdictionary<string,person>(); //returns a person based on a string index PublicPerson This[stringName] { Get{return(person) listpeople[name];} Set{listpeople[name]=value;} } ...}
4. Overloaded Indexer Method
An indexer method can be overloaded on a single class or struct.
// datatablecollection type, overloaded indexer Public This [string name] {get;} Public This [string name,string tablenamespace] {get;} Public This [int index] {get;}
5, Multidimensional Indexer: The typical representative is the DataTable in ADO
Private int New int [ten]; Public int This [int row,int colum]{/* take value or assignment from two-bit array */}
6. Define indexers on the interface type
In an interface-defined indexer, its implementation class can provide a custom implementation
Public Interface istringcontainer{ // This interface defines an indexer that returns a string based on a numeric index . [int index] {get; Set;}}
C # Indexer Method