I understand C # (2): Indexer

Source: Internet
Author: User

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.