【发布时间】:2015-08-20 08:58:09
【问题描述】:
我有一个字符串集合。例如,
string[] coll={"1", "2", "3" ..."100"..."150"...}
我对字符串集合有各自的方法,例如
void Method1, void Method2, void Method100
我选择了这样的适当方法:
string selector=string.Empty;
switch(selector)
{ case "1":
MethodOne();
break;
........
case "150":
Method150();
break;
}
上面的代码真的很无聊,我会在字符串集合{“150”...“250”...}中添加更多的字符串元素。 如何制作这样的:
string sel=col[55];
if(sel!=null)
// call here the respective method(method55)
我不想使用 switch 运算符,因为它会导致代码过剩。
【问题讨论】:
-
Dictionary<string,Action> -
@Eser 写下你的答案,我会标记它。
-
@StepUp 你有类似的答案。最好接受它。
-
@Eser 我已经给出了答案。您的答案是较早的,也是第一个。这是公平的。
标签: c# .net-4.0 switch-statement console-application