zoukankan      html  css  js  c++  java
  • C#学习笔记七索引器

    索引器

     

    namespace 索引器

    {

        class Program

        {

            static void Main(string[] args)

            {

                Person p1 = new Person();

                p1[1] = "小明";

                Console.WriteLine(p1[1] + p1[2]);

                Console.ReadKey();

            }

        }

        public class Person

        {

            private string firstName = "大毛";

            private string secondName = "二毛";

     

            //下面这个就是索引器

            public string this[int index]//索引可以有不止一个参数,setget可以只有一个

            {

                set

                {

                    if (index == 1)

                    {

                        firstName = value;

                    }

                    else if (index == 2)

                    {

                        secondName = value;

                    }

                    else

                    {

                        throw new Exception("输入的值不合适");

                    }

                }

                get

                {

                    if (index == 1)

                    {

                        return firstName;

                    }

                    else if (index == 2)

                    {

                        return secondName;

                    }

                    else

                    {

                        throw new Exception("输入的值不合适");

                    }

                }

            }

        }

    }

  • 相关阅读:
    【Java】Java 序列化的高级认识
    【随笔】感同身受
    【教训】徐小平:不要用兄弟情谊来追求共同利益,要用共同利益追求兄弟情谊
    【面试】惠普IT电面
    【面试】中兴
    【面试】国金证券
    【298】◀▶ IDL 系统过程&函数
    【297】IDL 过程、函数&关键字参数
    【296】Python 默认 IDE 修改
    【295】暗黑表格模板及相关
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2152397.html
Copyright © 2011-2022 走看看