问题描述
我只是为这个冗长的切换案例感到厌烦,我觉得那里已经有了更好的解决方案,如果没有的话,请以一种您可能曾经用过的创造性方法来帮助我
非常感谢分享大家!
Hi,
I''m just sick of this lengthy switch case drama and I have a feeling there are already better solutions out there, if not, please help me with a creative way you may have used once to make it easier and shorter..
Many thanks for sharing guys!
推荐答案
enum MyEnum { Foo, Bar, Baz }
bool method(MyEnum val)
{
switch (val)
{
case MyEnum.Foo:
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
case MyEnum.Bar:
throw new Exception("Bar!");
case MyEnum.Baz:
Console.Beep();
Console.WriteLine("Beep.");
return false;
default:
throw new ArgumentOutOfRangeException("val");
}
}
一种可能性是取出case语句的主体并从中取出方法.
One possibility is to take the bodies of the case statements and make methods out of them.
bool method(MyEnum val)
{
switch (val)
{
case MyEnum.Foo:
return FooMethod();
case MyEnum.Bar:
return BarMethod();
case MyEnum.Baz:
return BazMethod();
default:
throw new ArgumentOutOfRangeException("val");
}
}
bool FooMethod()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
bool BarMethod()
{
throw new Exception("Bar!");
}
bool BazMethod()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
另一种方法是创建要委托的键的字典,并执行字典查找/调用来代替开关.
Another is to create a dictionary of key to delegate, and do a dictionary lookup/invoke in place of a switch.
delegate bool BoolFunc();
readonly Dictionary<MyEnum, BoolFunc> methods;
MyClassConstructor()
{
methods = new Dictionary<MyEnum, BoolFunc>();
methods.Add(MyEnum.Foo, FooMethod);
methods.Add(MyEnum.Bar, BarMethod);
methods.Add(MyEnum.Baz, BazMethod);
}
bool method(MyEnum val)
{
BoolFunc funcToCall;
if (!methods.TryGetValue(val, out funcToCall))
throw new ArgumentOutOfRangeException("val");
return funcToCall.Invoke();
}
bool FooMethod()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
bool BarMethod()
{
throw new Exception("Bar!");
}
bool BazMethod()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
另一种方法是寻找一种面向对象的方法,在其中通过覆盖抽象/虚拟方法来区分行为,以代替某个地方的switch语句.
Another is to look for an object-oriented way where you differentiate behavior by overriding an abstract/virtual method, in place of a switch statement somewhere.
bool method(MyNonEnumClass val)
{
return val.DoYourThing();
}
abstract class MyNonEnumClass
{
public abstract bool DoYourThing();
}
class FooClass : MyNonEnumClass
{
public override bool DoYourThing()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
}
class BarClass : MyNonEnumClass
{
public override bool DoYourThing()
{
throw new Exception("Bar!");
}
}
class BazClass : MyNonEnumClass
{
public override bool DoYourThing()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
}
一切都取决于. :)
All depends, though. :)
public enum Operation <br />{<br /> Close,<br /> Save,<br />}<br />public void Action(Operation action)<br />{<br /> if (_dictionary.ContainsKey(action))<br /> {<br /> _dictionary[action]();<br /> }<br />}<br />public void Register(Operation operation, Action action)<br />{<br /> _dictionary.Add(operation, action);<br />}
然后,您可以像这样添加实现:
Then, you can add your implementation like this:
Register(Operation.Close, delegate(){ this.Close(); });
如您所见,调用此方法完全不需要开关.
As you can see, calling this method removes the need for a switch altogether.
这篇关于比Switch Case更好的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!