C# 开关:案例不会落入其他案例限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# switch: case not falling through to other cases limitation
提问by Mike Fielden
This question is kind of an add-on to this question
这个问题是种的附加到这个问题
In C#, a switch case cannot fall through to other cases, this causes a compilation error. In this case I am just adding some number to the month total for the selected month and each subsequent month thereafter. (simple example, not meant to be real)
在 C# 中,switch case 不能落入其他 case,这会导致编译错误。在这种情况下,我只是在所选月份和此后的每个月份的月份总数中添加一些数字。(简单的例子,并不意味着是真实的)
switch (month)
{
case 0:
add something to month totals
case 1:
add something to month totals
case 2:
add something to month totals
default:
break;
}
Is there a logical alternative to this in C# without having to write out a ton of if statements?
在 C# 中是否有一个合乎逻辑的替代方案,而不必写出大量的 if 语句?
if (month <= 0)
add something to month
if (month <= 1)
add something to month
if (month <= 2)
add something to month
.... etc
采纳答案by Ben Scheirman
Often times when you see the noise from a huge switch statement or many if statements that might fall into more than one block, you're trying to suppress a bad design.
很多时候,当您看到来自一个巨大的 switch 语句或许多可能属于多个块的 if 语句的噪音时,您是在试图抑制一个糟糕的设计。
Instead, what if you implemented the Specification pattern to see if something matched, and then act on it?
相反,如果您实现规范模式以查看是否匹配,然后采取行动呢?
foreach(MonthSpecification spec in this.MonthSpecifications)
{
if(spec.IsSatisfiedBy(month))
spec.Perform(month);
}
then you can just add up different specs that match what you're trying to do.
然后你可以添加与你想要做的事情相匹配的不同规格。
It's hard to tell what your domain is, so my example might be a little contrived.
很难说你的域是什么,所以我的例子可能有点做作。
回答by Brian Warshaw
There is already a question addressing this topic:
已经有一个问题解决了这个话题:
C# switch statement limitations - why?
EDIT:
编辑:
My main purpose in pointing that out, gentlebeasts, is that two questions of near-identical name add confusion to the pool of questions.
我指出这一点的主要目的是,绅士们,两个名字几乎相同的问题给问题池添加了混乱。
回答by Adam Davis
Are you adding constants? If so, maybe something like this would work(C syntax):
你在添加常量吗?如果是这样,也许这样的事情会起作用(C 语法):
const int addToTotals[] = {123, 456, ..., 789};
for(i=month;i<12;i++)
totals += addToTotals[i];
You can do a similar thing with variable or function pointers if you need more complex statements than add constant to totals for each month following.
如果您需要更复杂的语句而不是将常量添加到每个月的总数中,您可以使用变量或函数指针执行类似的操作。
-Adam
-亚当
回答by jwarzech
In C# switch statements you can fall through cases only if there is no statement for the case you want to fall through
在 C# 的 switch 语句中,只有当没有针对您想要通过的案例的语句时,您才能通过案例
switch(myVar)
{
case 1:
case 2: // Case 1 or 2 get here
break;
}
However if you want to fall through with a statement you must use the dreaded GOTO
但是,如果您想通过语句失败,则必须使用可怕的 GOTO
switch(myVar)
{
case 1: // Case 1 statement
goto case 2;
case 2: // Case 1 or 2 get here
break;
}
回答by svv
Write the switch cases in reverse order
以相反的顺序编写 switch case
case 2:
case 1:
case 0:
break;
default:
Hope that helps!
希望有帮助!