創作內容

1 GP

C#中switch做法

作者:怪鳥│2017-04-10 11:41:45│巴幣:2│人氣:524
坊間程式書籍介紹C# switch時,大部分都說是if else的延伸。例如:

//程式片段A
int foo=某數;
if(foo==1)
{
Console.WriteLine("A");
}
else if(foo==2)
{
Console.WriteLine("B");
}
else if(foo==3)
{
Console.WriteLine("C");
}
else
{
Console.WriteLine("not matchs");
}


//程式片段B
int foo=某數;
switch (foo)
{
case 1:
Console.WriteLine("A");
break;
case 2:
Console.WriteLine("B");
break;
case 3:
Console.WriteLine("C");
break;
default:
Console.WriteLine("not matchs");
break;
}

程式片段A和程式片段B執行結果是一樣的。但是再看看下面的

//程式片段C
string str= "";
if(typeof(bool) == str.GetType()))
{
Console.WriteLine("is bool");
}
else if(typeof(string) == str.GetType()))
{
Console.WriteLine("is string");
}
else
{
Console.WriteLine("not matchs");
}

//程式片段D
string str= "";
switch(str.GetType())
{
case typeof(bool):
Console.WriteLine("is bool");
break;
case typeof(string):
Console.WriteLine("is string");
break;
default:
Console.WriteLine("not matchs");
break;
}
程式片段C能改寫成程式片段D嗎?
答案是不行:
因為CIL在轉換switch時並不"一定"以if else形式轉換,多數情況下是產生一個case資料組成的jump table後,從jump table搜尋條件符合後執行對應記憶體位址的程式。這樣做是為了效能考量;CIL會依據case資料的連續度和多寡來決定以哪種方式轉換。因為可能轉換成jump table,所以case資料必然是(常數)constant組成。例如以下程式就可以執行:
//程式片段E
public static class ActionStatus
{
public const string Create = "A";
public const string Edit = "E";
public const string Delete = "D";
public const string Search = "S";
}

string str= "A";
switch(str)
{
case ActionStatus.Search:
Console.WriteLine("do not change db data");
break;
default:
Console.WriteLine("will change db data");
break;
}

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3539718
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:C#

留言共 5 篇留言

貓貓風 ฅ●ω●ฅ
此功能主要適用來縮短程式碼

要不然一堆if else 維護起來也很麻煩

04-16 11:22

羽翼ˊ● ω ●ˋ
通常是拿來這樣用沒錯
我個人昰不會拿可能變動(後續維護)的程式碼做switch
如果要改成非單一變數判斷的就得重寫

04-16 12:55

騎Mogeko的普羅
那請問一個疑問
如果只是if else 2選1的情況的話

switch還會比 if else省效能嘛!?

07-12 12:44

怪鳥
你沒弄懂switch的作法。目前暫時沒時間解釋給你聽。請自行google07-14 14:29
semmyenator
程式片段C不能直接重寫為程式片段D,因為C#中的switch語句要求case標籤是編譯時常數。 在程式片段 C 中,if-else 語句中的條件是基於變數的執行時間類型,該變數不是編譯時常數。

在程式片段 D 中,switch 語句需要編譯時常數表達式作為 case 標籤。 C# 中的 typeof 運算子在執行時傳回 Type 對象,該物件不是編譯時常數。 因此,不允許在 switch 語句中使用 typeof。

要使用 switch 語句實現與程式片段 C 類似的功能,您需要使用不同的方法。 一種可能的解決方案是使用字典將類型映射到相應的字串,然後在字典鍵上使用 switch 語句。

10-06 21:49

semmyenator
這是一個例子:

string str = "";
Dictionary<Type, string> typeMappings = new Dictionary<Type, string>
{
{ typeof(bool), "is bool" },
{ typeof(string), "is string" }
};

switch (str.GetType())
{
case Type t when typeMappings.ContainsKey(t):
Console.WriteLine(typeMappings[t]);
break;
default:
Console.WriteLine("not matches");
break;
}

在這個範例中,我們定義了一個字典 typeMappings,它將 Type 物件對應到對應的字串。 然後,在 switch 語句中,我們使用 Type 模式匹配來檢查字典是否包含類型作為鍵。 如果是,我們檢索相應的字串並列印它。 否則,我們列印“not matches”。

10-06 21:49

我要留言提醒:您尚未登入,請先登入再留言

1喜歡★stbird 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:Regex 筆記 可輸入... 後一篇:swift String...

追蹤私訊切換新版閱覽

作品資料夾

MoeTakoヾ(●゜▽゜●)♡
來一起陪雪糕畫畫喵✧◝(⁰▿⁰)◜✧看更多我要大聲說昨天20:24


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】