国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#索引器簡單實例代碼

2020-01-24 03:31:55
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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();

            //關聯數組的方式訪問get方法

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

            //關聯數組的方式訪問set方法

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

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

            Console.ReadLine();

        }

    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 禹城市| 海伦市| 八宿县| 张家界市| 许昌市| 平舆县| 曲麻莱县| 沙河市| 新乐市| 绵竹市| 金阳县| 鄄城县| 涿鹿县| 简阳市| 额敏县| 武宣县| 武穴市| 仁寿县| 安西县| 海门市| 南通市| 寿阳县| 静宁县| 台东市| 晋中市| 广南县| 宜君县| 龙岩市| 奎屯市| 清徐县| 竹山县| 葫芦岛市| 五原县| 崇文区| 交城县| 玉溪市| 会东县| 体育| 青海省| 邯郸市| 乌鲁木齐县|