小弟我是菜鸟,近日看了关于索引器的一些文章,多是讲索引器的语法和概念。可很少有讲索引器到底应该在什么场景下使用?到底索引器可以解决实际项目什么问题?
谢谢各位大哥大姐。小弟愚钝,请举例解释!小弟跪谢ORZ

解决方案 »

  1.   

    最简单的例子...
    string s="一二三";s[0]=='一';
    s[1]=='二';
    s[2]=='三';
      

  2.   

    在说个例子如下:public class example
    {
        private string[] _strExample = new string[] {"字符串1", "字符串2", "..."};    public string StrExample[index]
        {
            get {return _strExample[index];}
        }
    }在使用时调用如下:  example objExample = new example();
      string result = objExample.StrExample[1];这时result 的结果为:"字符串2";
      

  3.   

    谢谢楼上的,这个我知道。我就不知道为什么要用索引器?我把 example 类下的数组定义为公有的,我直接访问好不好。呵呵谢谢阿
      

  4.   

    just a example...    public class example
        {
            private Dictionary<string, decimal> fruits = new Dictionary<string, decimal>();        public example()
            {
                fruits.Add("Apple", 1.2);
                fruits.Add("Orange", 1.6);
                fruits.Add("Banana", 1.1);
                fruits.Add("Pear", 1.4);
                fruits.Add("Watermelon", 0.9);
                fruits.Add("Grapes", 2.0);
            }        public decimal this[string index]
            {
                get { return fruits[index]; }
            }
        }
      

  5.   

    楼上的正解,索引器其实是一种特殊的属性。
    将程序完善如下:
    using System;
    using System.Collections.Generic;
    namespace MyEx
    {
        //定义一个类
        public class example
        {
            //泛型,水果字典
            private Dictionary<string, double> fruits = new Dictionary<string, double>();
            //构造函数,水果字典中添加数据
            public example()
            {
                fruits.Add("Apple", 1.2);
                fruits.Add("Orange", 1.6);
                fruits.Add("Banana", 1.1);
                fruits.Add("Pear", 1.4);
                fruits.Add("Watermelon", 0.9);
                fruits.Add("Grapes", 2.0);
            }
            //类的索引器,通过类或对象加下标来得到fruits的值
            public double this[string index]
            {
                get { return fruits[index]; }
            }
        }
        public class Demo
        {
            public static void Main(string[] args)
            {
                example ex = new example();
                //输出结果是1.2
                Console.WriteLine(ex["Apple"]);
            }
        }
    }
      

  6.   

           我也有同楼主一样的困惑```就比如三楼举的这个例子:growleaf 发表于:2008-02-18 23:45:253楼 得分:0 
    在说个例子如下: public   class   example 

            private   string[]   _strExample   =   new   string[]   {"字符串1",   "字符串2",   "..."};         public   string   StrExample[index] 
            { 
                    get   {return   _strExample[index];} 
            } 
    } 在使用时调用如下:     example   objExample   =   new   example(); 
        string   result   =   objExample.StrExample[1]; 这时result   的结果为:"字符串2"; 
    如果我把_strExample声明为public的不就可以这样使用了吗?
        example   objExample   =   new   example(); 
        string   result   =   objExample._strExample[1]这时result   的结果为:"字符串2";    问题就是在这里,没有使用索引器,但是同样能够实现了索引器的功能.所以想问问各位前辈,索引器一般在什么样的特定环境下才使用,向上面这个例子,不用索引同样能够实现,我为何还非要用索引呢???请教请教,感谢感谢!!!
     
      

  7.   

    如果你要实现这样的赋值方式:Student[0]="zhangsan";Student["Name"]="zhangsan";这两段代码都会给学生类的姓名赋值,这是个简单的例子(不太恰当),这用数据可以实现吗?另外,如果程序需要对象数组,用索引器和数组它们在内存分配是不同的,索引器的内存管理是在对象里面的进行了,会有效率一些,
    你可以比较一下索引器与属性和对象数组的差别就会明白
      

  8.   

    索引器和公开数组字段的关系=属性和公开字段的关系
    索引器可以做参数检查,可以写各种逻辑,可以接受非int型的索引,这些数组都做不到