-- C#
C# Switch Fall-through
어린왕자악꿍
2011. 5. 13. 10:14
C#에서 Switch문을 쓸 경우 주의해야 할 사항이 있는데, 바로 Fall-through 적용 여부이다.
Fall-through라는 것은 case문에서 break를 걸지 않을 경우, 다음 case문이 실행되는 것을 말한다.
C#에 경우에는 Fall-through를 지원하지 않으므로 기억하고 있어야 한다.
JAVA (Fall-through 지원)
C# (Fall-through 미지원)
참조 : http://www.harding.edu/fmccown/java_csharp_comparison.html
Fall-through라는 것은 case문에서 break를 걸지 않을 경우, 다음 case문이 실행되는 것을 말한다.
C#에 경우에는 Fall-through를 지원하지 않으므로 기억하고 있어야 한다.
JAVA (Fall-through 지원)
greeting = age < 20 ? "What's up?" : "Hello";
if (x < y)
System.out.println("greater");
if (x != 100) {
x *= 5;
y *= 2;
}
else
z *= 6;
int selection = 2;
switch (selection) { // Must be byte, short, int, char, or enum
case 1: x++; // Falls through to next case if no break
case 2: y++; break;
case 3: z++; break;
default: other++;
}
C# (Fall-through 미지원)
greeting = age < 20 ? "What's up?" : "Hello";만약 C#에서 Fall-through를 구현하려면 case문에서 goto문을 사용하여 구현이 가능하다.
if (x < y)
Console.WriteLine("greater");
if (x != 100) {
x *= 5;
y *= 2;
}
else
z *= 6;
string color = "red";
switch (color) { // Can be any predefined type
case "red": r++; break; // break is mandatory; no fall-through
case "blue": b++; break;
case "green": g++; break;
default: other++; break; // break necessary on default
}
참조 : http://www.harding.edu/fmccown/java_csharp_comparison.html