The index function is a further extension of attributes. It can control the reading and writing of multiple variables in an array. The popular point is that you can beat a cow in the mountains. You can also beat a cow directly, but it may be difficult to control the intensity. The indexer achieves this effect. Let's take a look at the implementation below.Code:
Code
Using System;
Class Indexerclass
{
Private Int [] Myarray = New Int [ 100 ];
Public Int This [ Int Index] // Indexer Declaration
{
Get
{ // Check the index limits.
If (Index < 0 | Index > = 100 )
Return 0 ;
Else
Return Myarray [Index];
}
Set
{
If ( ! (Index < 0 | Index > = 100 ))
Myarray [Index] = Value;
}
}
}
Public Class Mainclass
{
Public Static Void Main ()
{
Indexerclass B = New Indexerclass ();
// Call the indexer to initialize the elements #3 and #5.
B [ 3 ] = 256 ;
B [ 5 ] = 1024 ;
For ( Int I = 0 ; I <= 10 ; I ++ )
{
Console. writeline ( " Element #{0 }={ 1} " , I, B [I]);
}
}
}