索引器在语法上方便您创建客户端应用程序可将其作为数组访问的类、结构或接口。索引器经常是在主要用于封装内部集合或数组的类型中实现的。索引器不光用于封装,有时通过索引器来封装字段会带来意想不到的效果.
通过一个例子来说明索引.
如下的类:
public class Parameter
{
#region Fields
public double Hf1;
public double Hf2;
public double Hf3;
public double Hf4;
public double Hf5;
public double Hf6;
public double Hf7;
public double Hf8;
public double Hf9;
public double Ts1;
public double Ts2;
public double Ts3;
public double Ts4;
public double Ts5;
public double Ts6;
public double Ts7;
public double Ts8;
public double Ts9;
public double Tr;
public double Tcl1;
public double Tcl2;
public double Tcl3;
public double Tcl4;
public double Tcl5;
public double Tcl6;
public double Tcl7;
public double Tcl8;
public double Tcl9;
public double Icl;
public double _Ts;
public double _Tcl;
public double _Hf;
public double s;
public double _Tb;
public double _s;
#endregion
#region Members
#endregion
}
如果要访问类的字段,或者封装成属性,或者直接作为有字段来使用
有时如果要以字符串为索引来访问字段.这时定义一个索引器就会很方便
现在在类中添加一个索引器,添加索引器后的类如下:
public class Parameter
{
#region Fields
public double Hf1;
public double Hf2;
public double Hf3;
public double Hf4;
public double Hf5;
public double Hf6;
public double Hf7;
public double Hf8;
public double Hf9;
public double Ts1;
public double Ts2;
public double Ts3;
public double Ts4;
public double Ts5;
public double Ts6;
public double Ts7;
public double Ts8;
public double Ts9;
public double Tr;
public double Tcl1;
public double Tcl2;
public double Tcl3;
public double Tcl4;
public double Tcl5;
public double Tcl6;
public double Tcl7;
public double Tcl8;
public double Tcl9;
public double Icl;
public double _Ts;
public double _Tcl;
public double _Hf;
public double s;
public double _Tb;
public double _s;
#endregion
#region Members
#endregion
public double this[string paraid]
{
get
{
if (paraid == "Hf1")
return Hf1;
else if (paraid == "Hf2")
return Hf2;
else if (paraid == "Hf3")
return Hf3;
else if (paraid == "Hf4")
return Hf4;
else if (paraid == "Hf5")
return Hf5;
else if (paraid == "Hf6")
return Hf6;
else if (paraid == "Hf7")
return Hf7;
else if (paraid == "Hf8")
return Hf8;
else if (paraid == "Hf9")
return Hf9;
else if (paraid == "Ts1")
return Ts1;
else if (paraid == "Ts2")
return Ts2;
else if (paraid == "Ts3")
return Ts3;
else if (paraid == "Ts4")
return Ts4;
else if (paraid == "Ts5")
return Ts5;
else if (paraid == "Ts6")
return Ts6;
else if (paraid == "Ts7")
return Ts7;
else if (paraid == "Ts8")
return Ts8;
else if (paraid == "Ts9")
return Ts9;
else if (paraid == "Tr")
return Tr;
else if (paraid == "Tcl1")
return Tcl1;
else if (paraid == "Tcl2")
return Tcl2;
else if (paraid == "Tcl3")
return Tcl3;
else if (paraid == "Tcl4")
return Tcl4;
else if (paraid == "Tcl5")
return Tcl5;
else if (paraid == "Tcl6")
return Tcl6;
else if (paraid == "Tcl7")
return Tcl7;
else if (paraid == "Tcl8")
return Tcl8;
else if (paraid == "Tcl9")
return Tcl9;
else if (paraid == "Icl")
return Icl;
else if (paraid == "_Ts")
return _Ts;
else if (paraid == "_Tcl")
return _Tcl;
else if (paraid == "_Hf")
return _Hf;
else if (paraid == "s")
return s;
else if (paraid == "_Tb")
return _Tb;
else if (paraid == "_s")
return _s;
else if (paraid == "delta_Tb")
return _delta_Tb;
else
return MessageBox.Show(this.ToString() + "中不存在这样的成员:" + paraid, "成员不存在", MessageBoxButtons.OK).GetHashCode();
}
set
{
if (paraid == "Hf1")
Hf1 = value;
else if (paraid == "Hf2")
Hf2 = value;
else if (paraid == "Hf3")
Hf3 = value;
else if (paraid == "Hf4")
Hf4 = value;
else if (paraid == "Hf5")
Hf5 = value;
else if (paraid == "Hf6")
Hf6 = value;
else if (paraid == "Hf7")
Hf7 = value;
else if (paraid == "Hf8")
Hf8 = value;
else if (paraid == "Hf9")
Hf9 = value;
else if (paraid == "Ts1")
Ts1 = value;
else if (paraid == "Ts2")
Ts2 = value;
else if (paraid == "Ts3")
Ts3 = value;
else if (paraid == "Ts4")
Ts4 = value;
else if (paraid == "Ts5")
Ts5 = value;
else if (paraid == "Ts6")
Ts6 = value;
else if (paraid == "Ts7")
Ts7 = value;
else if (paraid == "Ts8")
Ts8 = value;
else if (paraid == "Ts9")
Ts9 = value;
else if (paraid == "Tr")
Tr = value;
else if (paraid == "Tcl1")
Tcl1 = value;
else if (paraid == "Tcl2")
Tcl2 = value;
else if (paraid == "Tcl3")
Tcl3 = value;
else if (paraid == "Tcl4")
Tcl4 = value;
else if (paraid == "Tcl5")
Tcl5 = value;
else if (paraid == "Tcl6")
Tcl6 = value;
else if (paraid == "Tcl7")
Tcl7 = value;
else if (paraid == "Tcl8")
Tcl8 = value;
else if (paraid == "Tcl9")
Tcl9 = value;
else if (paraid == "Icl")
Icl = value;
else if (paraid == "_Ts")
_Ts = value;
else if (paraid == "_Tcl")
_Tcl = value;
else if (paraid == "_Hf")
_Hf = value;
else if (paraid == "s")
s = value;
else if (paraid == "_Tb")
_Tb = value;
else if (paraid == "_s")
_s = value;
else if (paraid == "delta_Tb")
_delta_Tb = value;
else
MessageBox.Show(this.ToString() + "中不存在这样的成员:" + paraid, "成员不存在", MessageBoxButtons.OK);
}
}
}
这样就可以这样访问类:
Parameter parameter
double Hf1 = parameter["Hf1"];
double x;
parameter["Hf1"] = x;
通过一个例子来说明索引.
如下的类:
public class Parameter
{
#region Fields
public double Hf1;
public double Hf2;
public double Hf3;
public double Hf4;
public double Hf5;
public double Hf6;
public double Hf7;
public double Hf8;
public double Hf9;
public double Ts1;
public double Ts2;
public double Ts3;
public double Ts4;
public double Ts5;
public double Ts6;
public double Ts7;
public double Ts8;
public double Ts9;
public double Tr;
public double Tcl1;
public double Tcl2;
public double Tcl3;
public double Tcl4;
public double Tcl5;
public double Tcl6;
public double Tcl7;
public double Tcl8;
public double Tcl9;
public double Icl;
public double _Ts;
public double _Tcl;
public double _Hf;
public double s;
public double _Tb;
public double _s;
#endregion
#region Members
#endregion
}
如果要访问类的字段,或者封装成属性,或者直接作为有字段来使用
有时如果要以字符串为索引来访问字段.这时定义一个索引器就会很方便
现在在类中添加一个索引器,添加索引器后的类如下:
public class Parameter
{
#region Fields
public double Hf1;
public double Hf2;
public double Hf3;
public double Hf4;
public double Hf5;
public double Hf6;
public double Hf7;
public double Hf8;
public double Hf9;
public double Ts1;
public double Ts2;
public double Ts3;
public double Ts4;
public double Ts5;
public double Ts6;
public double Ts7;
public double Ts8;
public double Ts9;
public double Tr;
public double Tcl1;
public double Tcl2;
public double Tcl3;
public double Tcl4;
public double Tcl5;
public double Tcl6;
public double Tcl7;
public double Tcl8;
public double Tcl9;
public double Icl;
public double _Ts;
public double _Tcl;
public double _Hf;
public double s;
public double _Tb;
public double _s;
#endregion
#region Members
#endregion
public double this[string paraid]
{
get
{
if (paraid == "Hf1")
return Hf1;
else if (paraid == "Hf2")
return Hf2;
else if (paraid == "Hf3")
return Hf3;
else if (paraid == "Hf4")
return Hf4;
else if (paraid == "Hf5")
return Hf5;
else if (paraid == "Hf6")
return Hf6;
else if (paraid == "Hf7")
return Hf7;
else if (paraid == "Hf8")
return Hf8;
else if (paraid == "Hf9")
return Hf9;
else if (paraid == "Ts1")
return Ts1;
else if (paraid == "Ts2")
return Ts2;
else if (paraid == "Ts3")
return Ts3;
else if (paraid == "Ts4")
return Ts4;
else if (paraid == "Ts5")
return Ts5;
else if (paraid == "Ts6")
return Ts6;
else if (paraid == "Ts7")
return Ts7;
else if (paraid == "Ts8")
return Ts8;
else if (paraid == "Ts9")
return Ts9;
else if (paraid == "Tr")
return Tr;
else if (paraid == "Tcl1")
return Tcl1;
else if (paraid == "Tcl2")
return Tcl2;
else if (paraid == "Tcl3")
return Tcl3;
else if (paraid == "Tcl4")
return Tcl4;
else if (paraid == "Tcl5")
return Tcl5;
else if (paraid == "Tcl6")
return Tcl6;
else if (paraid == "Tcl7")
return Tcl7;
else if (paraid == "Tcl8")
return Tcl8;
else if (paraid == "Tcl9")
return Tcl9;
else if (paraid == "Icl")
return Icl;
else if (paraid == "_Ts")
return _Ts;
else if (paraid == "_Tcl")
return _Tcl;
else if (paraid == "_Hf")
return _Hf;
else if (paraid == "s")
return s;
else if (paraid == "_Tb")
return _Tb;
else if (paraid == "_s")
return _s;
else if (paraid == "delta_Tb")
return _delta_Tb;
else
return MessageBox.Show(this.ToString() + "中不存在这样的成员:" + paraid, "成员不存在", MessageBoxButtons.OK).GetHashCode();
}
set
{
if (paraid == "Hf1")
Hf1 = value;
else if (paraid == "Hf2")
Hf2 = value;
else if (paraid == "Hf3")
Hf3 = value;
else if (paraid == "Hf4")
Hf4 = value;
else if (paraid == "Hf5")
Hf5 = value;
else if (paraid == "Hf6")
Hf6 = value;
else if (paraid == "Hf7")
Hf7 = value;
else if (paraid == "Hf8")
Hf8 = value;
else if (paraid == "Hf9")
Hf9 = value;
else if (paraid == "Ts1")
Ts1 = value;
else if (paraid == "Ts2")
Ts2 = value;
else if (paraid == "Ts3")
Ts3 = value;
else if (paraid == "Ts4")
Ts4 = value;
else if (paraid == "Ts5")
Ts5 = value;
else if (paraid == "Ts6")
Ts6 = value;
else if (paraid == "Ts7")
Ts7 = value;
else if (paraid == "Ts8")
Ts8 = value;
else if (paraid == "Ts9")
Ts9 = value;
else if (paraid == "Tr")
Tr = value;
else if (paraid == "Tcl1")
Tcl1 = value;
else if (paraid == "Tcl2")
Tcl2 = value;
else if (paraid == "Tcl3")
Tcl3 = value;
else if (paraid == "Tcl4")
Tcl4 = value;
else if (paraid == "Tcl5")
Tcl5 = value;
else if (paraid == "Tcl6")
Tcl6 = value;
else if (paraid == "Tcl7")
Tcl7 = value;
else if (paraid == "Tcl8")
Tcl8 = value;
else if (paraid == "Tcl9")
Tcl9 = value;
else if (paraid == "Icl")
Icl = value;
else if (paraid == "_Ts")
_Ts = value;
else if (paraid == "_Tcl")
_Tcl = value;
else if (paraid == "_Hf")
_Hf = value;
else if (paraid == "s")
s = value;
else if (paraid == "_Tb")
_Tb = value;
else if (paraid == "_s")
_s = value;
else if (paraid == "delta_Tb")
_delta_Tb = value;
else
MessageBox.Show(this.ToString() + "中不存在这样的成员:" + paraid, "成员不存在", MessageBoxButtons.OK);
}
}
}
这样就可以这样访问类:
Parameter parameter
double Hf1 = parameter["Hf1"];
double x;
parameter["Hf1"] = x;