concept
Indexer allows objects in a class to be referenced as conveniently and intuitively as arrays. When an indexer is defined for a class, the class behaves like a virtual array.
The indexer can have a parameter list and can only act on instance objects, not directly on the class.
An instance of the class can be accessed using the array access operator ([ ]).
The declaration of the behavior of an indexer is somewhat similar to a property. Attributes can be used to define indexers using get and set accessors. But the property returns or sets a specific data member, while the indexer returns or sets a specific value of the object instance.
Define an indexer for a one-dimensional array:
element-type this[int index] { // get accessor get { // Return the value specified by index } // set accessor set { // Set the value specified by index } }
Tip: The indexer must be defined with this keyword, this is an object after class instantiation
Example:
using System; namespace C_Pro { public class Student { private string name; private string grade; public string Name { get {return name; } set {name = value; } } public string Grade { get {return grade; } set {grade = value; } } // Define indexer public string this[int index] { get { if (index == 0) return name; else if (index == 1) return grade; else return null; } set { if (index == 0) name = value; else if (index == 1) grade = value; } } static void Main(string[] args) { Student s = new Student(); s[0] = "Jeson"; s[1] = "First-year"; (); (); (); } } }
After running results:
Jeson
First-year
Reload indexer
The indexer can be overloaded. The indexer can also be declared with multiple parameters, and each parameter can be of a different type. There is no need to make the indexer an integer. C# allows the indexer to be of other types, for example, string type.
using System; namespace C_Pro { public class IndexedNames { private string[] namelist = {"a", "b", "c", "d"}; // Enter the index of namelist to return the corresponding value public string this[int index] { get { return namelist[index]; } set { namelist[index] = value; } } // Enter the value of namelist to return the corresponding index public int this[string name] { get { for (int i=0; i<; i++) { if (namelist[i] == name) return i; } return -1; } } static void Main(string[] args) { IndexedNames name = new IndexedNames(); (name[1]); (name["a"]); } } }
After running results:
b
0
The difference between indexer and array:
- The index value (Index) type of the indexer is not limited to an integer. The index value (Index) used to access the array must be an integer, while the index value type of the indexer can be defined as other types.
- Indexers allow overloading. A class is not limited to only one indexer. As long as the indexer's function signature is different, multiple indexers can be defined and its functions can be overloaded.
- An indexer is not a variable. The indexer does not directly define the data storage, but an array has it. The indexer has a Get and Set accessor.
Difference between indexer and attribute:
- The indexer is identified by this function signature, while the attribute is identified by a name, and the name can be arbitrary.
- Indexers can be overloaded, but attributes cannot be overloaded.
- Indexers cannot be declared with static, but attributes can. The indexer is always an instance member and therefore cannot be declared as static.
The above is about the detailed content of the indexer in C#. For more information about the indexer in C#, please pay attention to my other related articles!