An indexer is a special class member that allows objects to be accessed in an array-like manner, making the program look 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 collection members, the indexer will greatly simplify access to arrays or collection members.
The way of defining an indexer is somewhat similar to that of defining attributes, and its general form is as follows:
[Modifier] Data type this[Index type index]
{
get{//get the code for the attribute}
set{ //Code for setting properties}
}
Modifiers include public, protected, private, internal, new, virtual, sealed, override, abstract, extern.
A data type is the type that represents the array or collection element to be accessed.
The indexer type indicates which type of index the index uses to access array or collection elements, which can be an integer or a string; this indicates the array or collection member that operates on this object, which can be simply understood as the name of the indexer, so the indexer cannot have a user-defined name. For example:
class Z
{
//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 >= 100))
{
arr[index] = value;
}
}
}
2. Use of indexer
The array members of the instance of a class can be accessed through the indexer. The operation methods are similar to those of the array, and the general form is as follows:
Object name [index]
The indexed data type must be the same as the index type of the indexer. For example:
Z z=new z();
z[0]=100;
z[1]=101;
(z[0]);
Indicates that an object z is created first, and then the array elements in the object are referenced through an index.
3. Indexer in the interface
Indexers can also be declared in an interface. There are two differences between interface indexers and class indexers: one is that the interface indexer does not use modifiers; the other is that the interface indexer only contains accessor get or set and does not implement statements. The purpose of the accessor is to indicate whether the indexer is read-write, read-only or write-only. If it is read-write, neither the accessor get nor set cannot be omitted; if it is read-only, the set accessor is omitted; if it is write-only, the get accessor is omitted.
For example:
public interface IAddress
{
string this[int index]{get;set;}
string Address{get;set;}
string Answer();
}
Indicates that the declared interface IAddress contains 3 members: an indexer, an attribute and a method, where the indexer is readable and writeable.
4. Comparison between indexer and attributes
Indexers and properties are members of classes, and are very syntactically similar. Indexers are generally used in custom collection classes. Using indexers to operate collection objects is as simple as using arrays; properties can be used in any custom class, which enhances the flexibility of the field members of the class.
Properties
Allows calling methods, like public data members |
Allows calling methods on objects, as if objects are an array |
Accessible by a simple name |
Accessible through the indexer |
Can be a static member or an instance member |
Must be an instance member |
Its get accessor has no parameters |
Its get accessor has the same formal parameter table as the indexer |
Its set accessor contains implicit value parameters |
In addition to the value parameter, its set accessor also has the same formal parameter table as the indexer |