当前位置:编程学习 > C#/ASP.NET >>

C#的索引器实现过程是?

C#的索引器实现过程是? --------------------编程问答-------------------- public int this[int i]
{
  get{//...}
  set{//...}
} --------------------编程问答-------------------- 具体参考
http://blog.csdn.net/licheng19891020/archive/2009/12/09/4974516.aspx --------------------编程问答-------------------- namespace Study
{
    class Program
    {
        static void Main(string[] args)
        {
            ScoreIndex s = new ScoreIndex();
            s["张三", 1] = 90;
            s["张三", 2] = 100;
            s["张三", 3] = 80;
            s["李四", 1] = 60;
            s["李四", 2] = 70;
            s["李四", 3] = 50;
            Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]);
            Console.WriteLine("张三的所有成绩为:");
            ArrayList temp;
            temp = s["张三"];
            foreach (IndexClass b in temp) 
            {
                Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
            }
            Console.ReadKey();
        }
    }
    class IndexClass 
    {
        private string _name;
        private int _courseid;
        private int _score;
        public IndexClass(string _name, int _courseid, int _score) 
        {
            this._name = _name;
            this._courseid = _courseid;
            this._score = _score;
        }
        public string Name 
        {
            get { return _name; }
            set { this._name = value; }
        }
        public int CourseID 
        {
            get { return _courseid; }
            set { this._courseid = value; }
        }
        public int Score 
        {
            get { return _score; }
            set { this._score = value; }
        }
    }
    class ScoreIndex 
    {
        private ArrayList arr;
        public ScoreIndex() 
        {
            arr = new ArrayList();
        }
        public int this[string _name, int _courseid] 
        {
            get 
            {
                foreach (IndexClass a in arr) 
                {
                    if (a.Name == _name && a.CourseID == _courseid) 
                    {
                        return a.Score;
                    }
                }
                return -1;
            }
            set 
            {
                arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
            }
        }
        //重载索引器
        public ArrayList this[string _name] 
        {
            get 
            {
                ArrayList temp = new ArrayList();
                foreach (IndexClass b in arr) 
                {
                    if (b.Name == _name) 
                    {
                        temp.Add(b);
                    }
                }
                return temp;
            }
        }
    }
}

所有索引器都使用this关键词来取代方法名。Class或Struct只允许定义一个索引器,而且总是命名为this。 
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。 
get 访问器返回值。set 访问器分配值。 
this 关键字用于定义索引器。 
value 关键字用于定义由 set 索引器分配的值。 
http://msdn.microsoft.com/zh-cn/library/6x16t2tx.aspx --------------------编程问答-------------------- 不知道你说的是什么意思?是如何创建索引器?还是.........
 创建索引器如下
 public Student this[int index] //Student是一个类 包含属性Name
 {
get {return students[index];}
 }
public Student this[string name]
{
 get
 {
  int i;
  bool found=false;
  for(i=0,i<students.Lenth;i++)//students 是一个泛型集合 用来存储Student对象
  {
   if(students[i].Name==name)
   {
   found=true;
   break;
   }
  }
   if(found)
   {
   return students[i];
   }
   else
   {
   return null;
   } 
}
}
这就创建了一个索引器用Student创建的对象可以通过下标索引访问类的对象 也可以通过Name 访问

--------------------编程问答-------------------- 索引器就是名字叫this的带有参数的属性。
可以用属性修改索引器名。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,