索引器到底该怎么使用?看书没看明白.

解决方案 »

  1.   

    看看msdn
    搜索下内容很多
    public class Person
        {
            private string name;
           private string password;
            public string Name
            {
                set { name = value; }
                get { return name; }
            }
            public string Password
            {
                set { password = value; }
                get { return password; }
            }
            public string this[int index]
            {
                get
                {
                  return name;
                }
                set
                {
                   if (index == 0) name = value;
                   else if (index == 1) password = value;
                }
            }
        }
      

  2.   

    关于索引器,给你做了一个小小的例子,希望可以能够带来参考意义。
    本示例通过一个班级的学生信息的包装,使用索引器来做。先创建一个简单的学生类using System;
    using System.Collections.Generic;
    using System.Text;namespace CSharpTest
    {
        /// <summary>
        /// 学生信息类
        /// </summary>
        public class Student
        {
            private int id;
            /// <summary>
            /// 设置或获取学生的编号
            /// </summary>
            public int Id
            {
                get { return id; }
                set { id = value; }
            }
            private string name;
            /// <summary>
            /// 设置或获取学生的姓名
            /// </summary>
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
        }
    }创建学生类的索引器,并让索引器支持迭代,让索引器可以像一个集合或者数组一样使用
    using System;
    using System.Collections.Generic;
    using System.Text;namespace CSharpTest
    {
        /// <summary>
        /// 这个类是用来保存学生类的集合
        /// 这个类的内部有一个索引器,用
        /// 来向这个集合中添加和读取一个
        /// 学生。
        /// </summary>
        public class StudentCollection
        {
            private List<Student> students = new List<Student>();        //
            //1.为什么要使用索引器呢?
            //因为这样可以更加直观(至少看起来像)的用一个类来作为
            //Student元素的集合,这个例子里面,感觉StudentCollection
            //这个类从名字上一看就是Student类的集合,这个类里面肯定
            //都装的是Student对象(当然也可以有其他元素,不过这个就
            //没有索引器的意义了.
            //2.不用索引器可不可以呢?
            //也是可以的,如果在另一个类中也有一个List集合,这里面
            //也是装的Student对象,但是你声明的时候就是声明:
            //private List<Student> students = new List<Student>();
            //如果用索引器来声明的话,就像这样:
            //StudentCollection students = new StudentCollection();
            //实现同样的功能。
            //        /// <summary>
            /// 索引器
            /// </summary>
            /// <param name="studentId">学生的编号</param>
            /// <returns></returns>
            public Student this[int id]
            {
                get
                {
                    foreach (Student stu in this.students)
                        if (stu.Id == id)
                            return stu;
                    return null;
                }
                set
                {
                    for (int i = 0; i < this.students.Count; i++)
                    {
                        if (this.students[i].Id == value.Id)
                        {
                            this.students[i] = value;
                            return;
                        }
                    }
                    this.students.Add(value);
                }
            }        /// <summary>
            /// 这个方法使索引器可以像集合或者数组一样直接迭代
            /// </summary>
            /// <returns></returns>
            public IEnumerator<Student> GetEnumerator()
            {
                foreach (Student stu in this.students)
                    yield return stu;
            }        /// <summary>
            /// 索引器其实更像属性,又像一个函数
            /// 索引器可以重载
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public Student this[string name]
            {
                get
                {
                    foreach (Student stu in this.students)
                        if (stu.Name == name)
                            return stu;
                    return null;
                }
                set
                {
                    for (int i = 0; i < this.students.Count; i++)
                    {
                        if (this.students[i].Name == value.Name)
                        {
                            this.students[i] = value;
                            return;
                        }
                    }
                    this.students.Add(value);
                }
            }
        }
    }然后创建一个测试类:using System;
    using System.Collections.Generic;
    using System.Text;namespace CSharpTest
    {
        class Test
        {
            public void Main()
            {
                //声明一般的学生集合对象
                StudentCollection class1_students = new StudentCollection();
                //
                //添加测试数据
                //
                Student stu1 = new Student();
                stu1.Id = 1;
                stu1.Name = "John";
                Student stu2 = new Student();
                stu2.Id = 2;
                stu2.Name = "Mick";
                Student stu3 = new Student();
                stu1.Id = 3;
                stu1.Name = "Ann";
                class1_students[1] = stu1;
                class1_students[2] = stu2;
                class1_students[3] = stu3;            foreach (Student stu in class1_students)
                {
                    Console.WriteLine("学号:" + stu.Id + "\t" + "姓名:" + stu.Name + "\n");
                }
            }
        }
    }