[英]C# check if string is equal to const name
I'm not sure if this is possible at all or if I should use something else then const. 我不确定这是否完全可能,或者我是否应该使用const之后的其他方式。
I have about 50 const strings, with all a unique value. 我有大约50个const字符串,所有的字符串都具有唯一值。 The program will read a string which is one of the const names. 程序将读取一个字符串,该字符串是const名称之一。
what I want: 我想要的是:
private const string AllPlace1 = "1355,-203,-4,0.002551732,0.705572185,0.708626711,0.003092848,-1,0,0,0";
private const string MoveDown1 = "1355,-203,-24,0.002551735,0.705572183,0.708626713,0.00309285,-1,0,0,0";
private const string Free1 = "1355,-108,-24,0.002551719,0.705572218,0.708626678,0.003092837,-1,0,0,0";
When the string "AllPlace1"
is given the system should print out the value of const AllPlace1
. 当给出字符串"AllPlace1"
,系统应打印出const AllPlace1
的值。
of course, I can write something like this for all possibilities, but that is not what I want to do for 50 possible values. 当然,我可以为所有可能的情况编写这样的内容,但这并不是我想为50个可能的值所做的。
if (args[3] == "AllPlace1")
WriteLine(AllPlace1);
else if (args[3] == "MoveDown1")
WriteLine(MoveDown1);
etc
You could use a dictionary instead: 您可以改用字典:
static readonly Dictionary<string, string> NameValueMapper = new Dictionary<string, string>{
{ "AllPlace1", "1355,-203,-4,0.002551732,0.705572185,0.708626711,0.003092848,-1,0,0,0"},
{ "MoveDown1", "1355,-203,-24,0.002551735,0.705572183,0.708626713,0.00309285,-1,0,0,0"},
{ "Free1" , "1355,-108,-24,0.002551719,0.705572218,0.708626678,0.003092837,-1,0,0,0"},
};
... ...
if (NameValueMapper.TryGetValue(args[3], out string value))
{
WriteLine(value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.