Let's take a look at the declaration format of the index indicator first:
Attributes Indexer-modifiers Indexer-declarator
{Accessor-declarations}
The modifier indexer-modifier that an index indicator can use are:
New
Public
Protected
Internal
Private
Virtual
Sealed
Override
Abstract
A pair of curly braces "{}" is an access declaration between index indicators that uses the GET keyword and the Set keyword to define the read and write permissions to the element being indexed.
For example, the following example prints out a list of team members.
Program Listing 13-4:
Using System;
Class Team
{
string s_name=new string[8];
public string This[int nindex]
{
get{return
s_name[nindex];
}
set{
s_name[nindex]=value
}
}} Class Test
{public
static void Main () {Team
t1=new team ();
for (int i=0;i<6;i++)
Console.WriteLine (T1[i]);
}
In many cases, some data information should be private to instances of the class or class, and you need to restrict access to that information. And we sometimes do not want such data to be completely closed to the outside world. As with attributes, an index indicator gives us another way to control access rights.