2012-05-02 102 views
2

我有以下幾點:我可以簡化此案例嗎?

switch (id.Substring(2, 2)) 
{ 
    case "00": return("14"); 
    case "01": return("19"); 
    case "02": return("19"); 
    case "03": return("19"); 
    case "1F": return("19"); 
    case "04": return("17"); 
    case "05": return("18"); 

} 

不知道這個,但我有辦法可以結合「01」,「02」,「03」和「1F」爲一體?

+1

的技術術語,你在尋找什麼是「秋天通過「 –

+3

Fyi,'return'不是一個函數調用,所以你不需要在返回值周圍加括號。 – ThiefMaster

+0

@KshitijMehta,C#不支持實際的轉場,只有特定的情況下,只有物品有任何代碼。 – svick

回答

11
switch (id.Substring(2, 2)) 
    { 
     case "00": return("14"); 
     case "01": 
     case "02": 
     case "03": 
     case "1F": return("19"); 
     case "04": return("17"); 
     case "05": return("18"); 
    } 

基本上只要你不添加任何代碼(因爲落空不支持),您可以使用多種測試值相同的結果路徑。

+0

「goto case'19'」在表演和練習中的表現如何? – Independent

+1

表現:很好;練習......好吧,有些人對'goto'感到非常不安 - 我非常務實,不介意,但通常會有更整潔的寫法 –

3

另一種方法是創建具有殼體條款(00,01等)作爲鍵和值的字典爲14,19等

然後在該切換。

0

不知道這是否會增加任何價值當前的答案,但與JFS行 - 我會做以下...

  var result = new Dictionary<string, string>(); 

      result.Add("00","14"); 
      result.Add("01", "14"); 
      result.Add("02", "14"); 
      result.Add("03", "14"); 
      result.Add("1F", "19"); 
      result.Add("04", "17"); 
      result.Add("05", "18"); 

      return result[id.Substring(2, 2)];