C# --索引器四个例子

C# 索引器四个例子:普通索引器,字符串索引器,接口定义索引器,泛型索引器。

//普通索引器

using System;

namespace Index1
{
    class TempRecord
    {
        private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F,
                                            61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
        System.DateTime date { get; set; }
        public int Length
        {
            get { return temps.Length; }
        }
        public float this[int index]
        {
            get
            {
                return temps[index];
            }

            set
            {
                temps[index] = value;
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            TempRecord tempRecord = new TempRecord();
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;
            for (int i = 0; i < 10; i++)
            {

                if (i < tempRecord.Length)
                {
                    System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
                }
                else
                {
                    System.Console.WriteLine("Index value of {0} is out of range", i);
                }
            }
        }
    }
}

效果:


//字符串,自定义索引器

using System;

namespace Index2
{
    class DayCollection
    {
        string[] days = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
        private int GetDay(string testDay)
        {
            int i = 0;
            foreach (string day in days)
            {
                if (day == testDay)
                {
                    return i;
                }
                i++;
            }
            return -1;
        }
        public int this[string day]
        {
            get
            {
                return (GetDay(day));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DayCollection week = new DayCollection();
            System.Console.WriteLine(week["星期五"]);
            System.Console.WriteLine(week["星期八"]);
        }
    }
}

效果:


//泛型索引器

using System;


namespace Index3
{
    class SampleCollection<T>
    {
        private T[] arr = new T[100];
        //this 关键字用于定义索引器。
        //索引器不必根据整数值进行索引,自定义决定如何定义特定的查找机制。
        //索引器可以有多个形参,例如当访问二维数组时。
        //索引器可被重载。
        public T this[int i]
        {
            get
            {
                return arr[i];
            }
            set
            {
                arr[i] = value;
            }
        }
    }

    // This class shows how client code uses the indexer
    class Program
    {
        static void Main(string[] args)
        {
            SampleCollection<string> stringCollection = new SampleCollection<string>();
            stringCollection[0] = "Hello, World";
            System.Console.WriteLine(stringCollection[0]);
        }
    }
}

效果:


//接口实现索引器

using System;

namespace Index4
{
    public interface ISomeInterface
    {
        int this[int index]
        {
            get;
            set;
        }
    }
    class IndexerClass : ISomeInterface
    {
        private int[] arr = new int[100];
        public int this[int index]
        {
            get
            {
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return arr[index];
                }
            }
            set
            {
                if (!(index < 0 || index >= 100))
                {
                    arr[index] = value;
                }
            }
        }
    }
    class MainClass
    {
        static void Main()
        {
            IndexerClass test = new IndexerClass();
            test[2] = 4;
            test[5] = 32;
            for (int i = 0; i <= 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
            }
        }
    }
}

效果:



posted on 2012-04-11 20:44 青蛙學堂 阅读(1470) 评论(0)  编辑 收藏 引用 所属分类: Vs2008

只有注册用户登录后才能发表评论。
<2006年2月>
2930311234
567891011
12131415161718
19202122232425
2627281234
567891011

导航

统计

常用链接

留言簿(7)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜