Indexer is a new type of class member introduced by C #. It allows objects in the class to be referenced as conveniently and intuitively as arrays. The indexer is very similar to an attribute, but the indexer can have a parameter list and can only act on instance objects, rather than directly act on classes. The class that defines the indexer allows you to use members of the [] OPERATOR category class just like accessing arrays. (Of course, there are still many advanced applications, for example, Arrays can be mapped out through the indexer)
Syntax of The indexer:
1. It can take one or more parameters. 2. Use this as the index name. 3. parameterized member attributes: including the set and get methods. [Access modifier] data type this [data type identifier] {Get {}; set {};} example:
Public class indexsy {private string [] array; Public indexsy (INT num) {array = new string [num]; for (INT I = 0; I <num; I ++) {array [I] = "array" + I;} Public String This [int num] {get {return array [num];} set {array [num] = value ;}}/// indexsy Sy = new indexsy (10); response. write (SY [5]); // output array5
Multi-parameter instances
Public class indexsy {private string [] array; Public indexsy (INT num) {array = new string [num]; for (INT I = 0; I <num; I ++) {array [I] = "array" + I;} Public String This [int num, string con] {get {If (num = 6) {return con ;} else {return array [num]; }}set {If (num = 6) {array [num] = con ;} else {array [num] = value ;}}// method call indexsy Sy = new indexsy (10); Sy [5, "10"] = "Change set value"; response. write (SY [5, ""] + "" + Sy [6, "Change Internal Parameters"] + "" + Sy [8, ""]); // Replace the set value with the internal parameter array8,
Comparison between indexer and array:
(1) The index value (INDEX) Type of the indexer is unrestricted.
(2) Reload allowed by the Indexer
(3) The indexer is not a variable.
Differences between indexer and attributes
(1) The attribute is identified by name, and the indexer is identified by function
(2) The indexer can be overloaded and Its Attributes cannot.
(3) The indexer cannot be declared as static, and the attribute can be