1. There are two types of indexers: interface indexer and class indexer.
2. The indexer can be overloaded.
3. The indexer does not need to perform an index based on the integer. You can decide how to define a specific search mechanism.
3. The index can have multiple parameters, for example, when accessing a two-dimensional array.
4. The accessors of the interface indexer are different from those of the class indexer in the following aspects:
1) the interface accessors do not use modifiers.
2) the interface accessor has no body
Example:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. text. regularexpressions; </P> <p> namespace consolecsharp <br/> {<br/> Public interface iindex <br/> {<br/> string this [int Index] <br/> {<br/> get; <br/> set; <br/>}< br/> class indexer: iindex <br/>{< br/> private string [] arrstr; <br/> private int arrstrsize; <br/> Public int length <br/>{< br/> Get <br/>{< br/> return arrstrsize; <br/>}</P> <p> Public String This [int POS] <br/>{< br/> Get <br/> {< br/> If (Pos <0 | POS> arrstrsize) <br/> return arrstr [0]; <br/> return arrstr [POS]; <br/>}< br/> set <br/> {<br/> If (Pos <0 | POS> arrstrsize) <br/> arrstr [0] = value; <br/> arrstr [POS] = value; <br/>}< br/> Public int this [String STR] <br/>{< br/> Get <br/>{< br/> int COUNT = 0; <br/> for (INT I = 0; I <arrstrsize; I ++) <br/> {<br/> If (arrstr [I] = Str) <br/> count ++; <br/>}< br/> return count; <br/>}< br/> set <br/> {<br/> for (INT I = 0; I <arrstrsize; I ++) <br/>{< br/> If (arrstr [I] = Str) <br/>{< br/> arrstr [I] = value. tostring (); <br/> break; <br/>}< br/> Public bool this [int I, int J] <br/> {<br/> Get <br/> {<br/> return arrstr [I] = arrstr [J]; <br/>}< br/> # region constructor <br/> Public indexer (INT size) <br/>{< br/> arrstrsize = size; <br/> arrstr = new string [size]; <br/> arrstr. initialize (); <br/>}< br/> Public indexer (Params string [] list) <br/>{< br/> arrstrsize = List. length; <br/> arrstr = new string [arrstrsize]; <br/> array. copy (list, arrstr, list. length ); <br/>}< br/> # endregion <br/>}</P> <p> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> indexer id = new indexer ("A", "B", "C", "D", "E "); <br/> ID [1] = "2"; <br/> console. writeline ("{0}", Id ["E"] = 2); <br/> console. writeline ("{0}", Id [1, 4]); <br/> console. writeline ("---------------"); <br/> for (INT I = 0; I <ID. length; I ++) <br/>{< br/> console. writeline (ID [I]); <br/>}< br/>