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
]);
    }
}

 

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

       等同于 Ontology

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

                           lis.add("lee");

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

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

posted on 2014-11-26 11:02  鸿鹄168  阅读(137)  评论(0编辑  收藏  举报

导航