An index indicator is essentially a parameter-included attribute, which is used in a similar way as an array. However, the index indicator parameter is not limited to int, and string can also be: Myweek = New Myweek ();
For ( Int I = 1 ; I <= Myweek. length; I ++ )
{
Console. writeline (myweek [I]);
}
The following is a simple example:
1. myweek class
/// <Summary>
/// The essence of an index indicator is that it contains parameter attributes,
/// The parameter is not limited to int, and can be a string.
/// </Summary>
Public Class Myweek
{
Public Int Length
{
Get { Return 7 ;}
}
/// <Summary>
/// Use int as the index indicator of the Parameter
/// </Summary>
/// <Param name = "Index"> </param>
/// <Returns> </returns>
Public String This [ Int Index]
{
Get
{
String Dayname;
Switch (Index)
{
Case 1 :
Dayname = " Monday " ;
Break ;
Case 2 :
Dayname = " Tuesday " ;
Break ;
Case 3 :
Dayname = " Wednesday " ;
Break ;
Case 4 :
Dayname = " Thursday " ;
Break ;
Case 5 :
Dayname = " Friday " ;
Break ;
Case 6 :
Dayname = " Saturday " ;
Break ;
Case 7 :
Dayname = " Sunday " ;
Break ;
Default :
Dayname = "" ;
Break ;
}
Return Dayname;
}
}
/// <Summary>
/// Use string as the index indicator of the Parameter
/// </Summary>
/// <Param name = "dayname"> </param>
/// <Returns> </returns>
Public Int This [ String Dayname]
{
Get
{
Dayname = Dayname. tolower ();
Switch (Dayname)
{
Case " Monday " :
Return 1 ;
Case " Tuesday " :
Return 2 ;
Case " Wednesday " :
Return 3 ;
Case " Thursday " :
Return 4 ;
Case " Friday " :
Return 5 ;
Case " Saturday " :
Return 6 ;
Case " Sunday " :
Return 7 ;
Default :
Return - 1 ;
}
}
}
}
2. Usage
Myweek = New Myweek ();
For ( Int I = 1 ; I <= Myweek. length; I ++ )
{
Console. writeline (myweek [I]);
}
String Dayname;
For ( Int I = 1 ; I <= Myweek. length; I ++ )
{
Dayname = Myweek [I];
Console. writeline (myweek [dayname]. tostring ());
}
3. output results
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
1
2
3
4
5
6
7
RelatedArticle: Implement the foreach Function
Address: http://www.cnblogs.com/anjou/archive/2007/07/06/808206.html