索引器使用示例

ilovewebservice發表於2011-03-29

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;

namespace Index
{
    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 CourseIndexer
    {
        private ArrayList arrCourseScore;
        public CourseIndexer()
        {
            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 Program
    {
        static void Main(string[] args)
        {
            CourseIndexer csi = new CourseIndexer();
            csi["張三",1]=90;
            csi["張三", 2] = 80;
            csi["張三",3] = 85;
            csi["李四", 1] = 80;
            Console.WriteLine(csi["張三", 2]);
            Console.WriteLine("張三的所有的課程成績為:");
            ArrayList tempArr;
            tempArr = csi["張三"];
            foreach(CourseScore cs in tempArr)
            {
                Console.WriteLine("姓名:" + cs.Name + " 課程編號:" + cs.CourseID + " 課程成績:" + cs.Score + "");
            }
            Thread.Sleep(5000);
        }
    }

    class Indexclass
    {
        //private string[] name = new string[10];
        //public string this[int index]
        //{
        //    get { return name[index]; }
        //    set { name[index] = value; }

        //}
        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)
        {
            //陣列類的使用
            //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]);
            //b["A001"] = "張三";
            //b["A002"] = "李四";
            //b["A003"] = "王五";
            //Console.WriteLine("b[A001]=" + b["A001"]);
            //Console.WriteLine("b[A002]=" + b["A002"]);
            //Console.WriteLine("b[A003]=" + b["A003"]);
            //呼叫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]);
        }
    }
}

相關文章