C# 中實現索引指標

來源:互聯網
上載者:User
索引指標本質就是一個含參數屬性,其使用方法和數組類似。但索引指標的參數並不只限於 int, string 也可以:MyWeek myWeek = new MyWeek();
for (int i = 1; i <= myWeek.Length; i++)
{
    Console.WriteLine(myWeek[i]);
}

下面是一個簡單的例子:

1. MyWeek 類

/// <summary>
/// 索引指標的實質是含參屬性,
///     參數並不只限於 int, string 也可以。
/// </summary>
public class MyWeek
{
    public int Length
    {
        get { return 7; }
    }

    /// <summary>
    /// 使用 int 作為參數的索引指標
    /// </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>
    /// 使用 string 作為參數的索引指標
    /// </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. 使用方法

MyWeek 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. 輸出結果
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
1
2
3
4
5
6
7

相關文章:實現 foreach 功能

本文地址:http://www.cnblogs.com/anjou/archive/2007/07/06/808206.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.