积极答复者
public string this[int index] 是什么意思?

问题
-
class SomeClass
{
public int name;
public string sname;
public SomeClass()
{
}
public SomeClass(string name)
{
this.sname = name;
}
public int Name
{
get { return name; }
set { name = value; }
}
public string Sname
{
get { return sname; }
set { sname = value; }
}
public string this[int index, string name, int a, int b, string c, double d]
{
get
{
return index.ToString() + name;
}
set
{
}
}
}这里边
public string this[int index, string name, int a, int b, string c, double d]
{
get
{
return index.ToString() + name;
}
set
{
}
}是什么意思啊??
答案
-
public string this [ int index, string name, int a, int b, string c, double d]
{
get
{
return index.ToString() + name;
}
set
{
}
}自定义索引器,这一般用于自定义集合类中并用来取得集合实例中的某一个元素
例如:
class ProductInfoCollection:ICollection<ProductInfo> { private List<ProductInfo> _items = new List<ProductInfo>(); public ProductInfoCollection() { } public ProductInfoCollection(string name) { //todo : 初始化 产品信息集合容器 _items } public ProductInfo this[int index, string name, int a, int b, string c, double d] { get { return GetProductInfoByInfo(index, name, a, b, c, d); } set { } } private ProductInfo GetProductInfoByInfo(int index, string name, int a, int b, string c, double d) { //todo : 根据产品的信息(int index, string name, int a, int b, string c, double d)在集合容器_item中查找特定产品信息 return null; } } public class ProductInfo { private string _index; public string Index { get { return _index; } set { _index = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } private int _a; public int A { get { return _a; } set { _a = value; } } private int _b; public int B { get { return _b; } set { _b = value; } } private double _c; public double C { get { return _c; } set { _c = value; } } public ProductInfo() { } }
不过这种索引器用法不太常规,一般是通过一个唯一能代表集合元素的key 属性来查找集合元素
I believe I can!- 已标记为答案 zhzhzh 2010年6月3日 2:55
全部回复
-
public string this [ int index, string name, int a, int b, string c, double d]
{
get
{
return index.ToString() + name;
}
set
{
}
}自定义索引器,这一般用于自定义集合类中并用来取得集合实例中的某一个元素
例如:
class ProductInfoCollection:ICollection<ProductInfo> { private List<ProductInfo> _items = new List<ProductInfo>(); public ProductInfoCollection() { } public ProductInfoCollection(string name) { //todo : 初始化 产品信息集合容器 _items } public ProductInfo this[int index, string name, int a, int b, string c, double d] { get { return GetProductInfoByInfo(index, name, a, b, c, d); } set { } } private ProductInfo GetProductInfoByInfo(int index, string name, int a, int b, string c, double d) { //todo : 根据产品的信息(int index, string name, int a, int b, string c, double d)在集合容器_item中查找特定产品信息 return null; } } public class ProductInfo { private string _index; public string Index { get { return _index; } set { _index = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } private int _a; public int A { get { return _a; } set { _a = value; } } private int _b; public int B { get { return _b; } set { _b = value; } } private double _c; public double C { get { return _c; } set { _c = value; } } public ProductInfo() { } }
不过这种索引器用法不太常规,一般是通过一个唯一能代表集合元素的key 属性来查找集合元素
I believe I can!- 已标记为答案 zhzhzh 2010年6月3日 2:55