zoukankan      html  css  js  c++  java
  • 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;
        }
    }

  • 相关阅读:
    event与WaitForSingleObject、MsgWaitForMultipleObjects等
    vc不包含MFC就不打印内存泄露?
    使用visual leak detector(vld)查找内存泄露
    C#里面中将字符串转为变量名
    如何编写nopCommerce插件
    object成员,不见了!
    NopCommerce 定制系列(一):增加 Sha256+Base64 加密
    c#中的二维数组与锯齿数组
    待搞清楚
    NopCommerce 2.5的部署
  • 原文地址:https://www.cnblogs.com/tianguook/p/1718860.html
Copyright © 2011-2022 走看看