Implementation process of the indexer in C #

Source: Internet
Author: User

C # the most interesting part of the language is the class indexer ). In short, the so-called indexer is a special type of attribute, through which you can reference your own
Class. Obviously, this function is particularly useful when creating collection classes. In some other cases, such as processing large files or abstracting some limited resources, the behavior that can make classes have similar arrays is also very
. 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
Assume that
If you have used VB6 to write a program, you should be familiar with the attribute method. The attribute method is actually a special class member and implements controlled access to the private class domain. The C # language has two attributes:
The first method is get, through which the value of the private domain can be returned, and the second is set, through which the value of the private domain can be set. For example, the following code creates a firstname
Attribute, which controls 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 );

For example
As you can see, the attribute declaration is more like a domain Declaration, but it also declares two special members. According to Microsoft's statement, it is the so-called access function (accessor ). When the right side of an expression
When you call an attribute or attribute as a parameter of another subroutine (or function), The get access function is called. Otherwise, when the attribute is called on the left side of the expression and the value parameter is passed implicitly, the private domain value is set.
In this case, 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

 

Below is its structure

<Modifier> <return type> This [argument list]
...{
Get
...{
// Get codes goes here
}
Set
...{
// Set codes goes here
}
}

Note:

Modifier: modifier, such as private, public, protected or internal

This: in C #, this is a special keyword that indicates the current instance of the referenced class. It indicates the index of the current class.

Argument list: the parameters of 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
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 add
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. In the same class
It may have more than one index (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.

 

Class samplecollection <t>
{
Private T [] arr = new T [100];
Public t this [int I]
{
Get
{
Return arr [I];
}
Set
{
Arr [I] = value;
}
}
}

// This class shows how client code uses the Indexer
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.