Explain indexer class.
- An indexer allows an object to be indexed such as an array.
- When you define an indexer for a class, this class behaves similar to a virtual array.
- You can then access the instance of this class using the array access operator ([ ]).
element-type this[int index]
{
get { // return the value specified by index }
set { // set the value specified by index }
}
Example of Indexer class
class Student
private int roll; public int Roll
{
get (return roll; } set roll value; }
Indexer Example
}
static void Main()
{
} private int[] marks new int[3];
public int this[int index]
{
get (return marks[index]; }
set (marks[index] = value; }
}
public double GetPercent()
{
double total 0.8;
foreach (int in marks)
{
}
total= total + m;
return total/marks.Length;
}
}
static void Main()
{
Student s1 = new Student ();
s1.Roll 1;
s1[0]= 50;
s1[1]=25;
s1[2]=40;
Console. WriteLine(s1.GetPercent() ) ;
s2[0]=20;
s2[1]=30;
s2[2]=50;
Console. WriteLine(s2.GetPercent() ) ;
Comments
Post a Comment