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

c#基础知识索引器

\代码
class SampleCollection<T>
{
    private T[] arr = new T[100];
    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]);
    }
}

 

 在这里我们看到,无非是实现了一个泛型算法

       等同于

        List<string> lit=new List<string>();

                           lis.add("lee");

                                                                     一般我们用 string lee{ get;set;}

 是用来访问一个对象的里面的属性,他这里相对于把对象自身当作一个属性来访问,这样以来就形成了一个新的访问形式,当然有很多方式可以替换。

    
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,