NET indexer usage example code

Source: Internet
Author: User

Indexer features

1. get accessors return values. Set accessors allocation value.
2. this keyword is used to define the indexer.
3. The value keyword is used to define the value allocated by the set indexer.
4. The indexer does not need to perform an index based on the integer. It is up to you to decide how to define a specific search mechanism.
5. The indexer can be overloaded.
6. The indexer can have multiple parameters, for example, when accessing a two-dimensional array.
7. The indexer allows objects to be indexed in a way similar to an array.

Sample Code

Copy codeThe Code is as follows:
Class SampleCollection <T>
{
Private T [] arr = new T [100];
Public T this [int I]
{
Get
{
Return arr [I];
}
Set
{
Arr [I] = value;
}
}
}
Class Program
{
Static void Main (string [] args)
{
SampleCollection <string> stringCollection = new SampleCollection <string> ();
StringCollection [0] = "Hello, World ";
System. Console. WriteLine (stringCollection [0]);
}
}

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.