C # Indexer Method

Source: Internet
Author: User

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

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.