Questions tagged «switch-expression»

4
C#8带有多个条件的开关表达式具有相同的结果
如何编写switch表达式以支持返回相同结果的多个案例? 对于版本8之前的C#,可以这样编写一个开关: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case 2: case 3: resultText = "one to three"; break; case 4: resultText = "four"; break; case 5: resultText = "five"; break; default: resultText = "unkown"; break; } 当我使用带有表达式语法的C#版本8时,就像这样: var switchValue = 3; var resultText …

11
C#如何在开关中使用枚举
我不知道如何结合枚举使用开关。您能否告诉我我在做什么错,以及如何解决?我必须使用一个枚举来制作基本计算器。 public enum Operator { PLUS, MINUS, MULTIPLY, DIVIDE } public double Calculate(int left, int right, Operator op) { int i = (int) op; switch(i) { case 0: { return left + right; } case 1: { return left - right; } case 2: { return left * right; } …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.