C # Introduction to indexer | C # method of Indexer | C # example of Indexer

Source: Internet
Author: User

The indexer is a special attribute. The following is an example of the simplest indexer.

Is to find the value corresponding to the index number given the index number, similar to the attribute array

Public String This [String configname]
{
Get
{
Return System. configuration. configurationmanager. configurettings [configname];
}

}

 

The indexer allows objects to be indexed in a way similar to an array.

Get accessors return values. Set accessors allocation value.

This keyword is used to define the indexer.

The value keyword is used to define the value allocated by the set indexer.

The indexer does not need to index based on the integer value. It is up to you to decide how to define a specific search mechanism.

The indexer can be overloaded.

The indexer can have multiple parameters, for example, when accessing a two-dimensional array.

C # the most interesting part of the language is the class indexer ). To put it simply, the so-called indexer is a special type of attributes. With these attributes, You can reference your own classes as you reference arrays. Obviously, this function is particularly useful when creating collection classes. In some other cases, such as processing large files or abstracting some limited resources, it is of course very useful to make classes behave like arrays. This article will guide you to set the class to use the indexer. However, first, let's give an overview of the attribute concept to understand the necessary background knowledge.

Attribute

Suppose you have written it in VB6ProgramSo you should be familiar with the attribute method. The so-called attribute method is actually a special class member, which implements controlled access to the private class domain. There are two attribute methods in C #. One is get, which can return the value of the private domain, and the other is set, through which the value of the private domain can be set. For exampleCodeFor example, a firstname attribute is created to control access to the private class member firstname:

Class person {
Private string firstname;
Public String firstname {
Get {return firstname ;}
Set {firstname = value ;}
}
}

The attribute declaration can be encoded as follows:
Person P = new person ();
P. firstname = "Lamont ";
Console. writeline (P. firstname );

As you can see, the attribute declaration is more like a domain Declaration, but it also declares two special members. According to Microsoft, it is called an access function (accessor ). When the call attribute or attribute on the right of an expression is used as a parameter of another subroutine (or function), The get access function is called. If the attribute is called on the left of the expression and the value parameter is implicitly passed to set the private domain value, the Set access function is called. You can create a read-only attribute by omitting the set access function, so that any attempt to set the attribute will produce a compilation error.

Benefits of using the Indexer

After talking about it for a long time, let's turn to the subject. Why should I go into this circle? In fact, this is because the class indexer is very similar to the attribute, and the Code also looks like this. The following is a class example with an indexer. The indexer returns a string:

Class sample {
Public String This [int Index] {
Get {return "You passed" + index ;}
}
}

Note that the attribute name here is this, which indicates the current instance of the reference class. The parameter list is included in square brackets rather than brackets. Also, this is a read-only index. To change it to the read/write type, I added a set access function. When defining the index, you do not have to use only one parameter. The indexer parameter can use any type, but int is usually the most reasonable type. The same class may also have more than one indexer (reload ).

After the sample class is defined as above, we can use the indexer as a default attribute, as shown below:

Sample S = new sample ();
Console. writeline (s [55]);

Attribute and Indexer

There are some differences between attributes and indexer:

Each attribute of the class must have a unique name, and each indexer defined in the class must have a unique signature) or the parameter list (so that the indexer can be reloaded ).

The attribute can be static, but the indexer must be an instance Member.

The parameters that can be accessed and passed to the indexer by the access function defined for the indexer, while the attribute access function has no parameters.

Interface

Similar arrays are often favored by the program implementers. Therefore, you can also define the indexer for the interface. The ilist and idictionary collection interfaces both declare the indexer to access the projects it stores.

When declaring the indexer for an interface, remember that the declaration only indicates the existence of the indexer. You only need to provide the appropriate access function, and do not need to include the range modifier. The following Code declares the indexer as part of the iimplementme interface:

Interface iimplementme {
String This [int Index]
{
Get;
Set;
}

The classes to be implemented must define the get and set access functions for the iimplementme index tool bodies.

The above is a basic overview of the indexer. Now you should have a deep understanding of the role of the indexer in your development.

The indexer allows instances of classes or structures to be indexed in the same way as arrays. The indexer is similar to an attribute, but its accessors use parameters.

In the following example, a generic class is defined and simple get and set accessors (as the method for allocating and retrieving values) are provided ). The program class creates an instance of this class for the storage string.

class samplecollection
{< br> private T [] arr = new T [100];
Public t this [int I]
{< br> Get
{< br> return arr [I];
}< br> set
{< br> arr [I] = value;
}< BR >}

// This class shows how client code uses the indexer
class Program
{< br> static void main (string [] ARGs)
{< br> samplecollection stringcollection = new samplecollection ();
stringcollection [0] = "Hello, world";
system. console. writeline (stringcollection [0]);

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.