C# Switch Fall-through

-- C# 2011. 5. 13. 10:14
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
C#에서 Switch문을 쓸 경우 주의해야 할 사항이 있는데, 바로 Fall-through 적용 여부이다.
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";
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
}
만약 C#에서 Fall-through를 구현하려면 case문에서 goto문을 사용하여 구현이 가능하다.

참조 : http://www.harding.edu/fmccown/java_csharp_comparison.html

'-- C#' 카테고리의 다른 글

C# DLL을 C, C++, MFC에서 쓰는 방법  (0) 2011.06.29
const vs readonly  (0) 2011.05.13
Dll Reference  (0) 2009.07.23
Alias 기능  (0) 2009.07.23
helloworld  (0) 2009.07.22
posted by 어린왕자악꿍