Use of the indexer in Asp. Net and asp.net

Source: Internet
Author: User

Use of the indexer in Asp. Net and asp.net

The indexer definition is similar to an attribute, but its functions and attributes are different. the indexer provides a special method for writing get and set accessors. the attribute can access the data of an object like an access field, and the indexer can enable users to access the metadata Class Members like an array.

 

I. indexing 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.

 

2. The way to define the index is similar to the way to define attributes. The general form is as follows:

 

[Modifier] data type this [index type index]

{

Get {// code for obtaining attributes}

Set {// set the Property Code}

}

Like the method, the indexer has five access protection levels: new, public, protected, internal, private, and four inheritance actions to modify virtual, sealed, override, abstract, and external indexer. these actions have no difference with the method. the only difference is that the indexer cannot be static, which is easy to understand in the semantics of object reference. it is worth noting that base [E] should be used to access the parent class indexer when override is used to implement the indexer.

 

Iii. Example code of the Indexer
 

C # code Replication
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]);    }}

 

4. Pay attention to the following when defining the index:

 

1. All indexers use the this keyword to replace method name. Class or Struct. Only one indexer can be defined and it is always named this.

2. The indexer allows instances of classes or structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters.

3. The indexer does not necessarily Index Based on Integer values. You can specify the parameter type according to programming requirements.

Related Article

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.