• 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("输入的值不合适");

                    }

                }

            }

        }

    }

  • 相关阅读:
    C++一个类对象的大小计算
    C++多态及其实现原理
    C ++内存管理
    C++ 面向对象的三大特性和五个原则
    Liunx系统下的进程与线程
    selenium 常用方法
    Jenkins UI 自动化持续化集成测试
    教育数据挖掘可投的会议及期刊整理
    SonarQube-7.9.1+SQL Server2017在Windows环境下的安装与配置
    win10+Anaconda3+PyCharm 2019.1+python3.7-tensorflow-gpu1.13.1(RTX2080深度学习环境配置)
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2152397.html
Copyright © 2020-2023  润新知