黑马程序员技术交流社区

标题: c#中属性和索引器之间有联系吗 [打印本页]

作者: lpz869    时间: 2014-5-26 00:57
标题: c#中属性和索引器之间有联系吗
本帖最后由 lpz869 于 2014-5-26 22:42 编辑

他们之间到底有关系吗,索引器在实际的工作当中用的多不多呢
作者: 许庭洲    时间: 2014-5-26 06:44
1. 索引器允许类或结构的实例就像数组一样进行索引。
2. 索引器类似于属性,不同之处在于它们的访问器采用参数。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class SampleCollection<T>
{
   private T[] arr = new T[100];
    publicT this[int i]
    {
       get
        {
           return arr;
       }
       set
        {
           arr = value;
        }
    }
}
// This class shows how client code uses theindexer
class Program
{
    staticvoid Main(string[] args)
    {
       SampleCollection<string> stringCollection = newSampleCollection<string>();
       stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
3.使用索引器可以用类似于数组的方式为对象建立索引。
4. get 访问器返回值。set 访问器分配值。
5.this 关键字用于定义索引器。
6. value 关键字用于定义由 set 索引器分配的值。
7.索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
8.索引器可被重载。
9.索引器可以有多个形参,例如当访问二维数组时。
作者: 袁晓俊    时间: 2014-5-26 09:28
我也学到了,,,,,,,,
作者: 陈君    时间: 2014-5-26 10:11
http://blog.sina.com.cn/s/blog_db23a6350101mjxw.html
可以看看的的这篇博文
作者: 张俊超    时间: 2014-5-26 13:39
索引的本质是属性
pubilc object this[int index]
{
    get{return this[index];}
    set{this[index]=value}
}
作者: /fendou    时间: 2014-5-26 15:25
学习了
作者: lpz869    时间: 2014-5-26 22:42
许庭洲 发表于 2014-5-26 06:44
1. 索引器允许类或结构的实例就像数组一样进行索引。
2. 索引器类似于属性,不同之处在于它们的访问器采用 ...

学到了,解释的真棒,谢谢了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2