The indexer is a special class member that allows an object to be accessed in an array-like manner, making the program more intuitive and easier to write.
1. Definition of Indexer
Class Members in C # can be of any type, including arrays and collections. When a class contains arrays and set members, the indexer greatly simplifies access to arrays or set members.
The way to define the indexer is similar to the way to define attributes. The general form is as follows: The indexer // this indicates to operate on the array or set members of the object, you can simply understand it as the name of the indexer. Therefore, when it is of the same type, remember to differentiate it by parameters.
Copy codeThe Code is as follows: // [modifier] data type this [index type index]
{
Get {// code for obtaining attributes}
Set {// set the Property Code}
}
For exampleCopy codeThe Code is as follows: public int this [int index]
{
Get {}
Set {}
}
An example is as follows:Copy codeThe Code is as follows: class Z
{
// An integer set that can accommodate 100 Integers
Private long [] arr = new long [100];
// Declare the Indexer
Public long this [int index]
{
Get
{// Check the index range
If (index <0 | index <= 100)
{
Return 0;
}
Else
{
Return arr [index];
}
}
Set
{
If (! (Index <0 | index <= 0 ))
{
Arr [index] = value;
}
}
}
2. Use of Indexer
The indexer can be used to access the array members of the class instance. The operation method is similar to that of the array. The general form is as follows:
Object Name [Index]
Modifiers include public, protected, private, internal, new, virtual, sealed, override, abstract, and extern. The index data type must be the same as the index type of The indexer. For example:Copy codeThe Code is as follows: Z z = new z ();
Z [0] = 100;
Z [1] = 101;
Console. WriteLine (z [0]); // indicates to create an object z first, and then reference the array elements in the object through the index.
3. Interface Indexer
You can also declare the indexer in the interface. There are two differences between the interface indexer and the class indexer: first, the interface indexer does not use modifiers; second, the interface indexer only contains the accessors get or set, and there are no implementation statements. The accessors are used to indicate whether the indexer can be read/written, read-only, or write-only. If it can be read/written, the accessors get or set cannot be omitted. If it is read-only, the set accessors are omitted. If the set accessors are write-only, the get accessors are omitted.
For example:
Copy codeThe Code is as follows: public interface IAddress
{
String this [int index] {get; set ;}
String Address {get; set ;}
String Answer ();
}
Indicates that the declared API IAddress contains three members: an indexer, an attribute, and a method. The indexer is readable and writable.
4. Comparison between the indexer and attributes
The indexer and attributes are both class members, which are similar in syntax. The indexer is generally used in custom collection classes. Using the indexer to operate collection objects is as simple as using arrays. attributes can be used in any custom class, it enhances the flexibility of field members of the class.
Attribute |
Indexer |
Call methods are allowed, just like public data members. |
The method on the object can be called, as if the object is an array |
Access by simple name |
Accessible through the Indexer |
It can be a static member or an instance Member. |
Must be an instance Member |
Its get accessors have no parameters. |
Its get accessors have the same parameters as the indexer. |
Its set accessors include the implicit value parameter. |
In addition to the value parameter, its set accessors also have the same parameters as the indexer. |