C # Indexer

Source: Internet
Author: User

1. Indexer: A class member in the C # language that allows an object to be referenced like an array , making the program look more intuitive and easier to write.

A simple case:

1   classTestidx2     {3          Public string  This[inti]4         {5             Get {6                 returnNames[i];7             }8             Set {9Names[i] =value;Ten             } One         } A         Private string[] names= {"Lily","Lucy","Tom"};//private Field names -}

1  Static voidMain (string[] args)2         {3Testidx Tidx =NewTestidx ();4             stringStr_name = tidx[0];//call the Get method, take the value5tidx[1] ="Sam";//Assigning a value to an indexer is actually called the set method6 Console.readkey ();7}
Testidx instances of this class obviously cannot directly manipulate the private field names, you can define an indexer in the class to achieve the purpose of operation names

2. Indexer Syntax:

Summarize the syntax of the indexer from this simplest case: [permission modifier] return value type this[argument type parameter name]

{

Get method

Set method

}

PS: Maybe I understand not deep enough, in the case of encapsulation, protection of member variables, storage collection class objects set to private, and then set to access their methods, then the access to their methods are also set to private, I can not understand, pativate type of indexer what role, in my opinion is idle egg pain.

3. Indexers and attributes are the same as C # syntax sugar, here's a comparison:

The attribute is identified by name, the name is arbitrarily taken, and the index is tagged with this keyword;

The indexer has a parameter list, and the general method differs in that it is written in the [] ..., so the indexer can be overloaded, the properties of course not possible;

Properties can be modified with static, indexers do not work, indexers can only be manipulated by an instance.

It is important to note that not only numeric indexes but also string types can be used:

String index case:

   classTESTIDX2 { Publicdictionary<string,string> dic =Newdictionary<string,string> () {{"A1","AAA"},            {"B1","BBB"}, {"C1","CCC" } };  Public string  This[stringSTR] {            Get {                returnDIC[STR]; }            Set{DIC[STR]= value; }        }    }
 static  void  Main (string  [] args) {TestIdx2 idx2  = new   TestIdx2 ();  string  stmp = Idx2[ " a1  " ]; // get method, value  idx2[ " b1  " ] =  " CCCCC    "; // set method, assigning a value          Console.readkey (); }

C # Indexer

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.