浅学C#(7)——索引器

版权声明:转载请注明出处 https://blog.csdn.net/le_17_4_6/article/details/86570397
索引器
  • 使用索引器的目的是为了能够像数组一样访问类中的数组型的对象
    通过对对象元素的下标进行索引,可以访问指定的对象
    索引器类似于属性,不同的是索引器有索引参数
    索引参数可以使用任何数据类型,但常用的是uint、int、ushort、short、string等
    不能使用foreach循环访问索引器
using System;
class ArrClass //没有索引器的类
{
    private readonly string name;
    public ArrClass(string name)
    {
        this.name = name;
    }
    public string Name
    {
        get
        { return name; }
    }

}
class IndexClass //带索引器的类
{
    private string[] name = new string[10];
    public string this[int index]
    {
        get { return name[index]; }
        set { name[index] = value; }
    }
}

    class Program
    {
        static void Main(string[] args)
        {
            // 数组类的使用
            ArrClass[] a = new ArrClass[10];
            a[0] = new ArrClass("张三");
            a[1] = new ArrClass("李四");
            a[2] = new ArrClass("王五");
            Console.WriteLine("a[0]=" + a[0].Name);
            Console.WriteLine("a[1]=" + a[1].Name);
            Console.WriteLine("a[2]=" + a[2].Name);         
            // 索引器的使用
            IndexClass b = new IndexClass();
            b[0] = "张三";
            b[1] = "李四";
            b[2] = "王五";
           Console.WriteLine("b[0]=" + b[0]);
            Console.WriteLine("b[1]=" + b[1]);
            Console.WriteLine("b[2]=" + b[2]);
            Console.ReadLine();
        }
    }


索引器的索引不一定是整数:

using System;
using System.Collections;
class IndexClass //带索引器的类
{
    private Hashtable name = new Hashtable();
    public string this[string index]
    {
        get { return name[index].ToString(); }
        set { name.Add(index, value); }
    }
}
class Program
{
    static void Main(string[] args)
    {        
        // 索引器的使用
        IndexClass b = new IndexClass();
        b["A001"] = "张三";
        b["A002"] = "李四";
        b["A003"] = "王五";
        Console.WriteLine("b[\"A001\"]=" + b["A001"]);
        Console.WriteLine("b[\"A002\"]=" + b["A002"]);
        Console.WriteLine("b[\"A003\"]=" + b["A003"]);
        Console.ReadLine();
    }
}

索引器允许重载:

using System;
using System.Collections;
class IndexClass //带索引器的类
{
    private Hashtable name = new Hashtable();
    public string this[int index] //A索引器
    {
        get { return name[index].ToString(); }
        set { name.Add(index, value); }
    }
    public int this[string aName] //B索引器
    {
        get
        {
            foreach (DictionaryEntry d in name)
            {
                if (d.Value.ToString() == aName)
                {
                    return Convert.ToInt32(d.Key);
                }
            }
            return -1;
        }
        set { name.Add(value, aName); }
    }
}
class Program
{
    static void Main(string[] args)
    {
        // 索引器的使用
        IndexClass b = new IndexClass();
       // 调用A索引器
        b[100] = "张三";
        b[200] = "李四";
        b[300] = "王五";
        Console.WriteLine("编号为100的员工是:" + b[100]);
        Console.WriteLine("编号为200的员工是:" + b[200]);
        Console.WriteLine("编号为300的员工是:" + b[300]);
      // 调用B索引器
        Console.WriteLine("张三的编号是:" + b["张三"]);
        Console.WriteLine("李四的编号是:" + b["李四"]);
        Console.WriteLine("王五的编号是:" + b["王五"]);
        b["马六"] = 400;
        b["钱七"] = 500;
      // 调用A索引器
        Console.WriteLine("编号为400的员工是:" + b[400]);
        Console.WriteLine("编号为500的员工是:" + b[500]);
        Console.ReadLine();
    }
}

  • 索引器的修饰符可以是new、public、protected、internal、private、virtual、sealed、override、abstract

  • 索引器与数组的不同点
    索引器的索引值类型不受限为整数
    索引器允许重载
    索引器不是一个变量
    索引器并不直接对应数据存储的地方
    索引器有get和set访问器,指明要读取或写入索引器元素时,需要执行的代码
    可以认为索引器的[]重载了数组的[]

  • 索引器与属性的不同点
    属性以名称来标识;索引器则以函数签名来标识
    索引器可以被重载,属性不能
    索引器不能声明为static,而属性可以

索引器可以使用多个参数访问:

using System;
using System.Collections;

class  CourseScore 
{
    private  string  name;
    private  int  courseID;
    private  int  score;
    public  CourseScore( string  name, int  courseID,int score)
    {
        this.name=name;
        this.courseID=courseID;
        this.score=score;
    }
    public string  Name
    {
        get  {  return  name; }
        set  { name=value; }
    }
    public  int  CourseID
    {
        get  {  return  courseID; }
        set  { courseID=value; }
    }
    public  int  Score
    {
        get  {  return  score; }
        set  { score=value; }
    }
}
class CourseScoreIndexer
{
    private ArrayList arrCourseScore;
    public CourseScoreIndexer()
    {
        arrCourseScore = new ArrayList();
    }
    public int this[string name, int courseID]
    {
        get
        {
            foreach (CourseScore cs in arrCourseScore)
            {
                if (cs.Name == name && cs.CourseID == courseID)
                {
                    return cs.Score;
                }
            }
            return -1;
        }
        set
        {
            arrCourseScore.Add(new CourseScore(name, courseID, value));
        }
    }
    public ArrayList this[string name]
    {
        get
        {
            ArrayList tempArr = new ArrayList();
            foreach (CourseScore cs in arrCourseScore)
            {
                if (cs.Name == name)
                {
                    tempArr.Add(cs);
                }
            }
            return tempArr;
        }
    }
}
class Test
{
    static void Main()
    {
        CourseScoreIndexer csi = new CourseScoreIndexer();
        csi["张三", 1] = 90;
        csi["张三", 2] = 80;
        csi["张三", 3] = 85;
        csi["李四", 1] = 76;
        Console.WriteLine(csi["张三", 3]);
        Console.WriteLine("张三的所有课程成绩为:");
        ArrayList tempArr;
        tempArr = csi["张三"];
        foreach (CourseScore cs in tempArr)
        {
            Console.WriteLine("姓名:" + cs.Name + "  课程编号:" + cs.CourseID + 
            "   分数:" + cs.Score);
        }
        Console.ReadLine();
    }
}

猜你喜欢

转载自blog.csdn.net/le_17_4_6/article/details/86570397