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;
        }
    }

  • 相关阅读:
    自从学会了搭建开源网站,妈妈再也不担心我找不到web自动化学习环境啦!
    领导要我6点下班前创建1000个有效的手机号,现在5点半了!教你用random模块10分钟搞定!
    python:字符串 扩展分片:第三个限制值
    python字符串:索引和分片
    测试基础:测试用例设计策略
    测试基础:软件测试思维方式
    面试整理:python列表面试习题
    面试整理:linux
    测试的一些思考
    python ConfigParse模块中的方法
  • 原文地址:https://www.cnblogs.com/tianguook/p/1718860.html
Copyright © 2011-2022 走看看