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

                    }

                }

            }

        }

    }

  • 相关阅读:
    【工具基础】google colab的使用
    【CV论文阅读】FoveaBox: Beyond Anchorbased Object Detector研读
    【ubuntu基础】使用清华镜像源更新安装工具
    【ubuntu基础】ubuntu 释放GPU内存
    Ubuntu18.04 rosmelodic opencv版本冲突问题解决
    多传感器标定工具
    pbootcms火车头采集教程(带免登录发布模块)
    家人之间的说话技巧
    pb如何修改授权码
    Spring Boot 异常处理,值得学习!
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2152397.html
Copyright © 2020-2023  润新知