The indexer allows instances of classes and structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters. It is called the property with parameters.
Simple indexer instance:
Comparison between the indexer and attributes:
Description: The attribute is identified by name, and the indexer is identified by function signature.
The indexer can be overloaded. Properties cannot be overloaded.
The attribute can be static. The indexer belongs to an instance Member and cannot be declared as static.
Multi-parameter indexer instance:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;
Namespace Study
{
Class Program
{
Static void main (string [] ARGs)
{
Scoreindex S = new scoreindex ();
S ["James", 1] = 90;
S ["James", 2] = 100;
S ["James", 3] = 80;
S ["Li Si", 1] = 60;
S ["Li Si", 2] = 70;
S ["Li Si", 3] = 50;
Console. writeline ("the score of course 3 numbered 1 is:" + s ["Zhang San", 1]);
Console. writeline ("all scores of Michael JACOB :");
Arraylist temp;
Temp = s ["James"];
Foreach (indexclass B in temp)
{
Console. writeline ("name:" + B. Name + "course No.:" + B. courseid + "score:" + B. Score );
}
Console. readkey ();
}
}
Class indexclass
{
Private string _ name;
Private int _ courseid;
Private int _ score;
Public indexclass (string _ name, int _ courseid, int _ score)
{
This. _ name = _ name;
This. _ courseid = _ courseid;
This. _ score = _ score;
}
Public string name
{
Get {return _ name ;}
Set {This. _ name = value ;}
}
Public int courseid
{
Get {return _ courseid ;}
Set {This. _ courseid = value ;}
}
Public int score
{
Get {return _ score ;}
Set {This. _ score = value ;}
}
}
Class scoreindex
{
Private arraylist arr;
Public scoreindex ()
{
Arr = new arraylist ();
}
Public int this [String _ name, int _ courseid]
{
Get
{
Foreach (indexclass A in ARR)
{
If (A. Name = _ name & A. courseid = _ courseid)
{
Return A. Score;
}
}
Return-1;
}
Set
{
Arr. Add (New indexclass (_ name, _ courseid, value); // arr ["Zhang San", 1] = 90
}
}
// Reload the Indexer
Public arraylist this [String _ name]
{
Get
{
Arraylist temp = new arraylist ();
Foreach (indexclass B in ARR)
{
If (B. Name = _ name)
{
Temp. Add (B );
}
}
Return temp;
}
}
}
}
Note:
All indexers use the this keyword to replace the method name. Class or struct can only define one indexer and is always named this.
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.
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.
The indexer can use a hundred-value subscript, while the array can only use an integer Subscript: for example, the following defines a string Index
Public int this [string name] {...}
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.