1. Description
The indexer allows instances of classes or structures to be indexed like arrays. The indexer is similar to an attribute, but its accessors use parameters.
You can use the indexer to create an index for an object in an array-like manner.
GetAccessors return values.SetThe value allocated by the accessor.
This keyword is used to define the indexer.
The value keyword is used to defineSetThe value allocated by the 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.
2. ExampleCode
Namespace csharpdemo {class program {static void main (string [] ARGs) {studentscore SC = new studentscore (); console. writeline ("score of the first student is {0}", SC [0]); console. readline () ;}/// <summary> // example of the indexer /// </Summary> public class studentscore {private double [] SC = {90, 88, 67, 41, 67, 72, 98}; private string [] names = {"Li", "Xu", "Zhang", "Wang", "Chen ", "Wu", "Hu"}; Public double this [int I] {get {return SC [I];} set {SC [I] = value ;}} // you can have multiple indexers. Public String This [string name] {get {return getname (name) ;}} private string getname (string name) is supported only when the parameters are different) {foreach (string s in names) {If (S = Name) {return s ;}} return string. empty ;}}}