Indexers
C # introduces the Indexers function, which allows us to process objects like arrays. In the C # community, we call it "smart arrays )". Defining the C # indexer is just as convenient as defining properties. (Here "property" refers to property ,.)
Below is its structure
<Modifier> <return type> this [argument list]
...{
Get
...{
// Get codes goes here
}
Set
...{
// Set codes goes here
}
}
Note:
Modifier: modifier, such as private, public, protected or internal
This: in C #, this is a special keyword that indicates the current instance of the referenced class. It indicates the index of the current class.
Argument list: the parameters of the indexer.
It has several rules:1. The ref and out parameters cannot be used.
2.There must be at least one parameter
3.Try to useInteger or string type parameters
The following is an example of an indexer.
Using System;
Using System. Collections;
Class MyClass
...{
Private string [] data = new string [5];
Public string this [int index]
...{
Get
...{
Return data [index];
}
Set
...{
Data [index] = value;
}
}
}
Class MyClient
...{
Public static void Main ()
...{
MyClass mc = new MyClass ();
Mc [0] = "Rajesh"; // mc is an object, but it is like an array, isn't it :)
Mc [1] = "A3-126 ";
Mc [2] = "Snehadara ";
Mc [3] = "Irla ";
Mc [4] = "Mumbai ";
Console. writeLine ("{0}, {1}, {2}, {3}, {4}", mc [0], mc [1], mc [2], mc [3], mc [4]);
}
}
The following are examples of inheritance, polymorphism, abstract class, and indexer.
Indexers and inheritance
Using System;
Class Base // The Base class has an indexer.
...{
Public int this [int indxer]
...{
Get
...{
Console. Write ("Base GET ");
Return 10;
}
Set
...{
Console. Write ("Base SET ");
}
}
}
Class Derived: Base
...{
}
Class MyClient
...{
Public static void Main ()
...{
Derived d1 = new Derived ();
D1 [0] = 10;
Console. WriteLine (d1 [0]); // use the indexer as usual
}
}
// Output
Base SET
Base GET
10
Indexers and Polymorphism
Using System;
Class Base
...{
Public virtual int this [int index] // There is a virtual Indexer
...{
Get
...{
Console. Write ("Base GET ");
Return 10;
}
Set
...{
Console. Write ("base set ");
}
}
}
Class derived: Base
...{
Public override int this [int Index]
...{
Get
...{
Console. Write ("derived get ");
Return 10;
}
Set
...{
Console. Write ("Derived Set ");
}
}
}
Class myclient
...{
Public static void main ()
...{
Base b1 = new derived ();
B1 [0] = 10;
Console. writeline (b1 [0]); // This is also the indexer that displays the derived object.
}
}
// Output
Derived SET
Derived GET
10
Abstract Indexers)
Using System;
Abstract class Abstract
...{
Public abstract int this [int index]
...{
Get;
Set;
}
}
Class Concrete: Abstract
...{
Public override int this [int index]
...{
Get
...{
Console. Write ("GET ");
Return 10;
}
Set
...{
Console. Write ("SET ");
}
}
}
Class MyClient
...{
Public static void main ()
...{
Concrete C1 = new concrete ();
C1 [0] = 10;
Console. writeline (c1 [0]);
}
}
// Output
Set
Get
10
Indexer and property)
1. The indexer uses its identifier as the parameter identifier. However, the attribute is identified by its name as a parameter.
Note: This [int I1] is identified by an int and belongs to the same indexer as this [int I2.
2. instance objects generally accessed by the indexer. However, attributes generally access static objects.
3. The indexer generally uses Arrays for access. However, attributes are generally accessed using object methods.
Note:Indexer:
Myclass MC = new myclass ();
MC [0] = "Rajesh ";
Attribute:
Myclass MC = new myclass ();
MC. Name = "Rajesh ";
Summary
1.The indexer can carry out overload, polymorphism, and abstraction, which is no different from general member functions.
2.The argument list can contain several parameters. This [int i1, int i2]
3.When a class has two or more indexers, the parameter list (argument list) must be different. This [int i1] And this [int i2] belong to the same indexer; this [int i1] And this [string s2] belong to different indexers;
4. C #There is no static indexer.