• C#中的索引器


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

    namespace ConsoleApplication2
    {
        class Program
        {
            public static void Main(string[] args)
            {
                Student[] stu = new Student[3];
                stu[0] = new Student("张三",23);
                stu[1] = new Student("李四",34);
                Stus stus = new Stus();
                stus.students = stu;
                Console.WriteLine(stus[1].age);
            }              
        }


        public class Student
        {
            public string name;
            public int age;
            public Student(string name, int age)
            {
                this.name = name;
                this.age = age;
            }
        }

        public class Stus
        {
            public Student[] students;
            public Student this[int index]
            {
                get
                {
                    return students[index];
                }
            }
            public Student this[string name]
            {
                get
                {
                    for (int i = 0; i < students.Length; i++)
                    {
                        if (students[i].name == name)
                        {
                            return students[i];
                        }
                    }
                    return null;
                }
            }
        }

    }

  • 相关阅读:
    Red hat 5挂载U盘
    Win7+VMware Workstation环境下的CentOS-Linux网络连接设置
    rand()随机数的产生
    phpmyadmin数据库导入出错
    dede忽略错误
    wamp
    网页地图map
    Redefining already defined constructor
    SCREAM:Error suppression ignored for
    Python+selenium之疑难点解决之去除readonly的限制
  • 原文地址:https://www.cnblogs.com/tianguook/p/1795997.html
Copyright © 2020-2023  润新知