好像很久沒有新文章產出了,今天就來發一篇。
狀況類似:同事來問為何這個 swith case 指述 會出現錯誤,如下
static string GetValue(int value) {
string result = string.Empty;
switch (value) {
case 1:
string s = "y";
break;
default:
string s = "hhh";
break;
}
return result;
}乍看之下,一時也是覺得為何?但在仔細推敲下,看出此範例的 case 中的 s 變數似乎不算區域變數,故會出現錯誤;
若需要這樣使用,。必須在 case 區段加入 { } 括號即可免疫。
0 Comments:
張貼留言