• C#中的索引器


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace ConsoleApplication3
    {
        class Program
        {

            static void Main(string[] args)
            {
                ClassInfo c = new ClassInfo();

                c.students = new Students();
                c.students.init();
                Console.WriteLine("我的名字叫{0},今年{1}岁了", c.students[0].name, c.students[0].age);
                Console.WriteLine("我的名字叫{0},今年{1}岁了", c.students["张三"].name, c.students["张三"].age);
            }
        }
        public class Students
        {
            Student[] students = new Student[3];

            public void init()
            {
                students[0] = new Student("张三", 24);
                students[1] = new Student("李四", 35);
                students[2] = new Student("王五", 42);
            }
            public Student this[string name]
            {
                get
                {

                    for (int i = 0; i < students.Length; i++)
                    {
                        if (students[i].name == name)
                        {
                            return students[i];

                        }

                    }
                    return null;
                }
            }

            public Student this[int index]
            {
                get { return students[index]; }
            }
        }
        public class ClassInfo
        {
            public Students students;
        }
        public class Student
        {
            public Student(string name, int age)
            {
                this.name = name;
                this.age = age;
            }
            public string name;
            public int age;
        }
    }

  • 相关阅读:
    很好的学习idea工具的教程
    事件绑定
    接口出现问题
    IDEA快捷方式
    源代码编译安装Python3.5.2
    CentOS7使用无线网卡
    MySql5.7.12设置log-bin
    报表传递参数控制数据权限
    python将png转为pkm
    WebGL纹理详解——压缩纹理的使用
  • 原文地址:https://www.cnblogs.com/tianguook/p/1718860.html
Copyright © 2020-2023  润新知