c#索引示例

来源:互联网 发布:猪八戒刷淘宝流量 编辑:程序博客网 时间:2024/04/28 21:14
class 例子
{
public int a, b, c;
public int this[string 字]
{
get
{
if (字 == "a") return a;
if (字 == "b") return b;
if (字 == "c") return c;
throw new Exception("...");
}
set
{
if (字 == "a") a = value;
else if (字 == "b") b = value;
else if (字 == "c") c = value;
else throw new Exception("...");
}
}
}
//使用举例:
例子 仅供参考 = new 例子();
仅供参考["a"]=0; 仅供参考["b"]=1; 仅供参考["c"]=2;

你也可以把索引定义为浮点数,窗体,阅读器,指针等等任何稀奇古怪的类型,只要它属于Object。