Implement index indicator in C #

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.