国产探花免费观看_亚洲丰满少妇自慰呻吟_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();

        }

    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资中县| 松江区| 澎湖县| 奉化市| 和田县| 桦南县| 定西市| 富蕴县| 安国市| 客服| 九龙城区| 财经| 仪陇县| 临湘市| 金乡县| 佛冈县| 阿拉善左旗| 通化市| 乐都县| 青田县| 封开县| 靖边县| 新泰市| 临沂市| 白河县| 灵丘县| 青神县| 开平市| 凯里市| 郴州市| 本溪| 咸阳市| 城市| 中卫市| 平乐县| 澎湖县| 澎湖县| 六枝特区| 新昌县| 郸城县| 休宁县|