欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#索引器簡單實例代碼

 更新時間:2013年03月20日 14:31:00   作者:  
打開.Net Framework源代碼隨便看幾個類,就會發(fā)現(xiàn)索引器的影子。索引器可以被重載,可以接收一個或者多個參數(shù),但是不可以定義為靜態(tài)的。可以用關(guān)聯(lián)數(shù)組的方式訪問索引器。

復(fù)制代碼 代碼如下:

public class Fruit

{

        string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";

        string orange = "a round fruit that has a thick orange skin and is divided into parts inside";

        string banana = "a long curved tropical fruit with a yellow skin";

        string apple = "a hard round fruit that has red, light green, or yellow skin and is white inside ";

        public string this[string fruitName]

        {

            get

            {

                switch (fruitName)

                {

                    case "peach":

                        return peach;

                    case "orange":

                        return orange;

                    case "banana":

                        return banana;

                    case "apple":

                        return apple;

                    default:

                        throw new Exception("wrong fruit name");

                }

            }

            set

            {

                switch (fruitName)

                {

                    case "peach":

                        peach = value;

                        break;

                    case "orange":

                        orange = value;

                        break;

                    case "banana":

                        banana = value;

                        break;

                    case "apple":

                        apple = value;

                        break;

                    default:

                        throw new Exception("wrong fruit name");

                }

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Fruit f = new Fruit();

            //關(guān)聯(lián)數(shù)組的方式訪問get方法

            Console.WriteLine(f["peach"]);

            //關(guān)聯(lián)數(shù)組的方式訪問set方法

            f["peach"] = "I like to eat peach.";

            Console.WriteLine(f["peach"]);

            Console.ReadLine();

        }

    }

相關(guān)文章

  • 簡單掌握Windows中C#啟動外部程序進程的方法

    簡單掌握Windows中C#啟動外部程序進程的方法

    這篇文章主要介紹了Windows中C#啟動外部程序進程的方法,例子中同時包括了進程關(guān)閉的方法,需要的朋友可以參考下
    2016-03-03
  • C#Url操作類封裝、仿Node.Js中的Url模塊實例

    C#Url操作類封裝、仿Node.Js中的Url模塊實例

    這篇文章主要介紹了C#Url操作類封裝、仿Node.Js中的Url模塊,實例分析了C#Url操作類封裝的技巧,非常具有實用價值,需要的朋友可以參考下。
    2016-10-10
  • WinForm遍歷窗體所有子控件的方法

    WinForm遍歷窗體所有子控件的方法

    這篇文章主要介紹了WinForm遍歷窗體所有子控件的方法,涉及C#遞歸遍歷相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • SQL+C#實現(xiàn)獲得當(dāng)前月的第一天與最后一天

    SQL+C#實現(xiàn)獲得當(dāng)前月的第一天與最后一天

    本文分享了SQL+C#獲得當(dāng)前月的第一天與最后一天的代碼實例,代碼簡潔,適合初學(xué)者參考。需要的朋友可以看下
    2016-12-12
  • 解讀C#中ReadString的一些小疑惑

    解讀C#中ReadString的一些小疑惑

    這篇文章主要介紹了關(guān)于C#中ReadString的一些小疑惑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • C#中整理了幾種字符串截取方法小結(jié)

    C#中整理了幾種字符串截取方法小結(jié)

    本文給大家整理了幾種字符串截取方法,?(Substring);(Remove);(Replace)方法和split方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • C#算法之實現(xiàn)阿姆斯特朗數(shù)

    C#算法之實現(xiàn)阿姆斯特朗數(shù)

    這篇文章介紹了C#實現(xiàn)阿姆斯特朗數(shù)的算法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • 獲取wince mac地址與IP地址解決方案

    獲取wince mac地址與IP地址解決方案

    由于需要進行身份的驗證,需要獲取移動終端的MAC地址,于是在網(wǎng)上進行搜索整理一番,現(xiàn)在將實現(xiàn)獲取MAC地址的方法與大家共享
    2012-12-12
  • c# 垃圾回收(GC)優(yōu)化

    c# 垃圾回收(GC)優(yōu)化

    這篇文章主要介紹了c# 垃圾回收(GC)優(yōu)化的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2021-02-02
  • 聊聊C#中的Mixin的具體用法

    聊聊C#中的Mixin的具體用法

    本文主要介紹了C#中的Mixin的具體用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05

最新評論