码迷,mamicode.com
首页 > Windows程序 > 详细

c#基础知识索引器

时间:2014-11-26 13:46:15      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   sp   strong   on   

代码
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;}

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

c#基础知识索引器

标签:style   blog   http   io   ar   color   sp   strong   on   

原文地址:http://www.cnblogs.com/joke168/p/4122726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!