Indexer in. NET/C #

Source: Internet
Author: User

From: http://www.cnblogs.com/csharp4/archive/2010/06/03/1750938.html

Why do we need something like indexer?

When we want to define a custom collection type, such as EmployeeCollection and ManagerCollection. because of the functional requirements, we use a generic collection class for storage in such a class. We also need to encapsulate some other necessary related methods in the class, for example, how to calculate the salary and how many working days the employee has. In this way, the indexing function of our generic storage set class is encapsulated in the class, and out of encapsulation considerations, it is impossible to directly use the EmployeeCollection. employeeList [] to directly access our internal storage, so we need to set the storage set class object to private, and then set the methods to access them: AddEmployee (), RemoveEmployee (), getEmployee. However, this practice is really not concise. We hope that the custom classes can be used in the same way as the collection classes themselves, and are implemented in a secure and controllable way:

 

EmployeeCollection [0] = new Employee ("name ");

...

Console. WriteLine (EmployeeCollection [I]. Name );

 

In view of the above requirements, just like some private member variables are encapsulated with property, we also need a mechanism to encapsulate the member collection classes, it is called the indexer ). As follows:

 

Public ElementsType this [int index]

{

Get {return EmployeeList [index];};

Ser {EmployeeList [index] = value ;};

}

 

Here we use the this [] keyword group. this is a required syntax. It must be the same. There is get and set in it. Is it like property? Haha ..... It indicates how the class set returns when the previous index structure is used, or how to set the specified element. The access method features the same as custom access in the class.

 

When we use a List <T> in a custom collection class that uses int indexes, we use the indexer to index the entire custom collection class, when our custom collection class uses a string index set type such as Dictionary <TKey, TValue>, our indexer can also be defined as a string index access method. Check the usage. The indexer can sometimes be used in a heavy load, or even define a multi-dimensional indexer ......

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.