• 索引


    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace _9.索引

    {

        class Program

        {

            static void Main(string[] args)

            {

                int[] values = { 3,5,7,9};

                int i = values[1];

                Person p1 = new Person();

                p1[1] = "小明";

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

                Console.WriteLine(p1["tom",3,9]);//索引也可以重载。

                Console.ReadKey();

            }

        }

        class Person

        {

            private string FirstName="大毛";

            private string SecondName="二毛";

            public string this[string name, int x, int y]

            {

                get

                {

                    return name + x + y;

                }

            }

            public string this[int index]//中括号里的就是索引。

            {

                get

                {

                    if (index == 1)

                    {

                        return FirstName;

                    }

                    else if (index == 2)

                    {

                        return SecondName;

                    }

                    else

                    {

                        throw new Exception("这是个错误的序号啊亲!");

                    }

                }

                set

                {

                    if (index == 1)

                    {

                        FirstName = value;

                    }

                    else if (index == 2)

                    {

                        SecondName = value;

                    }

                    else

                    {

                        throw new Exception("这是个错误的序号啊亲!");

                    }

                }

            }

        }

    }

  • 相关阅读:
    动态类型
    unlink与close关系
    Google 历年笔试面试30题
    UNIX网络编程 卷2 源代码使用
    centos安装telnet
    python中安装第三方模块
    Linux平台 Oracle 18c RAC安装Part1:准备工作
    RHEL7 配置iSCSI模拟环境
    Solaris 10主机名和IP地址步骤
    设置sqlplus不显示除查询结果外的信息
  • 原文地址:https://www.cnblogs.com/gyt-xtt/p/3639047.html
Copyright © 2020-2023  润新知