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:
Class Program
{
Static void Main (string [] args)
{
IndexClass a = new IndexClass ();
A [0] = "Chen San ";
A [1] = "Dai Si ";
A [2] = "5th ";
Console. WriteLine ("a [0] =" + a [0]);
Console. WriteLine ("a [1] =" + a [1]);
Console. WriteLine ("a [2] =" + a [2]);
Console. ReadKey ();
}
}
Class IndexClass
{
Private string [] name = new string [10];
Public string this [int index]
{
Get {return name [index];}
Set {this. name [index] = value ;}
}
}
Comparison between indexer and array:
The index value of the indexer is not subject to type restrictions. The index value used to access the array must be an integer, and the index can be another type of index value.
The indexer can be overloaded. A class can have multiple indexers.
The indexer does not directly store data for a variable. The indexer has get and set accessors.
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)
{
&