Possible Duplicate: Switch statement fallthrough in C#? The following code is illegal in C# because control cannot fall through from one case label to another. However, this behaviour is perfectly legal in C++. So, how would you go about coding the same behaviour in C#? enum TotalWords { One = 1, Two, Three, Four } public String SomeMethod(TotalWords totalWords) { St
可能重复: 在C#中切换语句贯穿始终? 下面的代码在C#中是非法的,因为控件不能从一个案例标签到另一个案例标签。 但是,这种行为在C ++中完全合法。 那么,你将如何在C#中编写相同的行为? enum TotalWords { One = 1, Two, Three, Four } public String SomeMethod(TotalWords totalWords) { String phrase = ""; switch (totalWords) { case TotalWords.Four:
This question already has an answer here: Switch statement fallthrough in C#? 14 answers This structure: case "LogOnManager": case "NormalUse": lblSwipeStatus.Text = "Pass ID BadgenNear The Reader"; break; basically means that the code in the second case will be used for either case . So both of the cases use the same code. Semantically this could be thought of as: case "LogOnM
这个问题在这里已经有了答案: 在C#中切换语句贯穿始终? 14个答案 这种结构: case "LogOnManager": case "NormalUse": lblSwipeStatus.Text = "Pass ID BadgenNear The Reader"; break; 基本上意味着第二种case的代码将用于任何一种case 。 所以这两种情况都使用相同的代码。 在语义上,这可以被认为是: case "LogOnManager" || "NormalUse": lblSwipeStatus.Text = "Pass ID BadgenNear The Reader";
It appears I need to use a break in each case block in my switch statement using C#. I can see the reason for this in other languages where you can fall through to the next case statement. Is it possible for case blocks to fall through to other case blocks? Thanks very much, really appreciated! Yes, you can fall through to the next case block in two ways. You can use empty cases, which d
看来我需要在使用C#的switch语句中的每个case块中使用一个中断。 我可以在其他语言中看到这个原因,您可以通过下一个案例陈述。 情况块是否可能落入其他情况块? 非常感谢,非常感谢! 是的,您可以通过两种方式进入下一个案例块。 您可以使用不需要休息的空白案例,也可以使用goto跳转到下一个(或任何)案例: switch (n) { case 1: case 2: case 3: Console.WriteLine("1, 2 or 3"); goto case 4;
Given a simple switch statement switch (int) { case 1 : { printf("1n"); break; } case 2 : { printf("2n"); } case 3 : { printf("3n"); } } The absence of a break statement in case 2, implies that execution will continue inside the code for case 3. This is not an accident; it was designed that way. Why was this decisions mad
给定一个简单的switch语句 switch (int) { case 1 : { printf("1n"); break; } case 2 : { printf("2n"); } case 3 : { printf("3n"); } } 案例2中没有中断陈述,意味着在案例3的代码中将继续执行。这不是偶然的; 它是这样设计的。 为什么做出这样的决定? 这提供了什么好处,而不是为块创建自动中断语义? 基本原理是什么? 许多答案似乎都把
Below is the logic for what I am trying to do .Can some one help me solve this using C#. string strMessage=string.empty; for (int i = 0; i < 20; ++i) { switch i { Case 1,2,7,5: strMessage="You Won"; break; Case 6,8,10,3: strMessage="You can try again"; break; } } Response.write(strMessage); Whenever the value of i is 1
下面是我正在尝试做的逻辑。可以帮助我使用C#来解决这个问题。 string strMessage=string.empty; for (int i = 0; i < 20; ++i) { switch i { Case 1,2,7,5: strMessage="You Won"; break; Case 6,8,10,3: strMessage="You can try again"; break; } } Response.write(strMessage); 每当i的值是1,2,7或5 strMessage =“你赢了”每当我的值是6,8,
How to handle multiple values inside one case ? So if I want to execute the same action for value "first option" and "second option" ? Is this the right way? switch(text) { case "first option": { } case "second option": { string a="first or Second"; break; } } It's called 'multiple labels' in the documentation, which can
如何处理一个case多个值? 所以如果我想为价值"first option"和"second option"执行相同的操作? 这是正确的方式吗? switch(text) { case "first option": { } case "second option": { string a="first or Second"; break; } } 它在文档中被称为“多个标签”,可以在MSDN的C#文档中找到。 一个switch语句可以包含任意数量的开关部分,每个部分可以有一个或
I haven't a living clue how to write this in c#. I know how to do it in Delphi. This is how I would do it in Delphi: Case Total of 80..100 : ShowMessage ('You got an A!'); 60..79 : ShowMessage ('You got a B!'); 50..59 : ShowMessage ('You got a C!'); 40..49 : ShowMessage ('You got a D!'); 0..39 : ShowMessage ('You got an E...'); end; I have read tutorials for th
我不知道如何在c#中编写它。 我知道如何在Delphi中做到这一点。 这是我如何在Delphi中完成的: Case Total of 80..100 : ShowMessage ('You got an A!'); 60..79 : ShowMessage ('You got a B!'); 50..59 : ShowMessage ('You got a C!'); 40..49 : ShowMessage ('You got a D!'); 0..39 : ShowMessage ('You got an E...'); end; 我已经阅读过这个教程,但我不知道如何以我需要的方式使用/编
In C#, the switch statement doesn't allow cases to span ranges of values. I don't like the idea of using if-else loops for this purpose, so are there any other ways to check numeric ranges in C#? You can use a HashTable respectively Dictionary to create a mapping of Condition => Action . Example: class Programm { static void Main() { var myNum = 12; var c
在C#中, switch语句不允许用例跨越值范围。 我不喜欢为此使用if-else循环的想法,那么有没有其他方法可以检查C#中的数字范围? 您可以分别使用HashTable Dictionary来创建Condition => Action的映射。 例: class Programm { static void Main() { var myNum = 12; var cases = new Dictionary<Func<int, bool>, Action> { { x => x < 3 , () =&
How can I use more than one constant for a switch case C#? Conceptually, I'm looking for something like this: switch(n) { case 1,2,3: //???? case 4: default: } 你不这样做,你会用掉落式的。 switch( n ) { case x: case y: case z: DoSomething( ); break; case blah: break; } A number of answers have stated that "fall through is legal if t
我怎样才能使用多个常量的开关情况C#? 从概念上讲,我正在寻找这样的东西: switch(n) { case 1,2,3: //???? case 4: default: } 你不这样做,你会用掉落式的。 switch( n ) { case x: case y: case z: DoSomething( ); break; case blah: break; } 许多答案都表示,“如果案件为空,则通过合法途径是合法的”。 这是不正确的。 这是考虑它的错误方式。 这是推理,
This question already has an answer here: Variable definition inside switch statement 5 answers Because memory will be allocated for int b but when the application is run " b = 20 " will never be evaluated. This is because your switch -statement will jump down to either case 1: 1 or default: , skipping the statement in question - thus b will be uninitialized and undefined behavior
这个问题在这里已经有了答案: switch语句中的变量定义5个答案 因为内存将被分配给int b ,但当应用程序运行时,“ b = 20 ”将不会被评估。 这是因为您的switch -statement会跳转到case 1: 1或default: :,跳过相关语句 - 因此b将被初始化并调用未定义的行为。 以下两个问题(以及他们接受的答案)将进一步帮助您寻求答案: 在定义被绕过时,如何使用变量? 2 为什么不能在switch语句中声明变量? 在编译源代码时