在说个例子如下: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";
谢谢楼上的,这个我知道。我就不知道为什么要用索引器?我把 example 类下的数组定义为公有的,我直接访问好不好。呵呵谢谢阿
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]; } } }
楼上的正解,索引器其实是一种特殊的属性。 将程序完善如下: 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"]); } } }
我也有同楼主一样的困惑```就比如三楼举的这个例子: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"; 问题就是在这里,没有使用索引器,但是同样能够实现了索引器的功能.所以想问问各位前辈,索引器一般在什么样的特定环境下才使用,向上面这个例子,不用索引同样能够实现,我为何还非要用索引呢???请教请教,感谢感谢!!!
string s="一二三";s[0]=='一';
s[1]=='二';
s[2]=='三';
{
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";
{
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]; }
}
}
将程序完善如下:
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"]);
}
}
}
在说个例子如下: 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"; 问题就是在这里,没有使用索引器,但是同样能够实现了索引器的功能.所以想问问各位前辈,索引器一般在什么样的特定环境下才使用,向上面这个例子,不用索引同样能够实现,我为何还非要用索引呢???请教请教,感谢感谢!!!
你可以比较一下索引器与属性和对象数组的差别就会明白
索引器可以做参数检查,可以写各种逻辑,可以接受非int型的索引,这些数组都做不到