2012-09-21 45 views
1

好的,如果我有一個字符串,我想等於基於多個條件的東西,那麼實現它的最佳方式是什麼?C#中的多個條件賦值?

僞碼

int temp = (either 1, 2, or 3) 
string test = (if temp = 1, then "yes") (if temp = 2, then "no") (if temp = 3, then "maybe") 

有一些簡潔的方式來做到這一點?你會怎麼做?

回答

5

您可以使用switch語句在其他的答案中提到,但也可以使用字典:

var dictionary = new Dictionary<int, string>(); 
dictionary.Add(1, "yes"); 
dictionary.Add(2, "no"); 
dictionary.Add(3, "maybe"); 

var test = dictionairy[value]; 

這種方法的方式比switch語句更加靈活和以往比嵌套tenary運算符的語句更易讀。

+0

+1,不是簡潔的問題,但更好的未來發展 – Steve

+0

+1使用字典... –

+0

雖然我最終在我的方法中使用了一個開關,但我選擇了這個答案,因爲我非常喜歡這個解決方案,而且我沒有知道這一點。我將在以後的所有實例中使用它。 – proseidon

2

您可以使用switch聲明

string temp = string.Empty; 
switch(value) 
{ 
    case 1: 
     temp = "yes"; 
     break; 
    case 2: 
     temp = "no"; 
     break; 
    case 3: 
     temp = "maybe"; 
     break; 
} 
11

使用switch

switch(temp) 
{ 
    case 1: 
     return "yes"; 
    case 2: 
     return "no"; 
    case default: 
     return "maybe"; 
} 
1

最簡潔的答案是嵌套的三元運算符

string test = (temp == 1 ? "yes" : (temp == 2 ? "no" : (temp == 3 ? "maybe" : ""))); 

如果溫度值僅1中, 2,3然後

string test = (temp == 1 ? "yes" : (temp == 2 ? "no" : "maybe")); 

當然這是一個簡潔的答案,這並不意味着它是最好的。 如果你不能排除這一點,將來你將需要更多的測試值,那麼最好使用@zeebonk答案中解釋的字典方法。

+0

你要哪一種偏愛,多個三元運算符或switch語句在這種情況下? –

+0

@LolCoder,好吧,OP要求一個簡潔的解決方案,所以三元運算符可能是答案。但是,如果您預計將來還有其他值需要測試,那麼三元運算符變得非常難以讀取。在這種情況下,我更喜歡switch語句,或者更好的是,Dictionary方法。 – Steve

+0

+1爲詞典方法....如果你會解釋這些東西在你的答案,我認爲這將是非常有用的未來的讀者..... –

4
string test = GetValue(temp); 

public string GetValue(int temp) 
{ 
    switch(temp) 
    { 
    case 1: 
     return "yes"; 

    case 2: 
     return "no"; 

    case 3: 
     return "maybe"; 

    default: 
     throw new ArgumentException("An unrecognized temp value was encountered", "temp"); 
    } 
} 
+0

我認爲這種方法拋出異常。如果採取,必須更好與InvalidEnumArgumentException,它會添加更多的上下文信息,你不覺得嗎? –

+0

@ RandolfR-F由於提供的參數是一個整數,我會堅持使用ArgumentException。如果函數正在接受枚舉並打開它,那麼InvalidEnumArgumentException將更合適。 –

0

明顯的答案將是開關的情況下

只是另一種味道:

int temp = x; //either 1,2 or 3 

string test = (temp == 1 ? "yes" : temp == 2 ? "no" : "maybe"); 
2

的基本思路是:

String choices[] = {"yes","no","maybe"}; 
string test = choices[temp-1]; 

還有的實際執行多種不同的方式它。取決於你的條件變量是什麼,你可能想將它實現爲某種鍵值列表。以Zeebonk的答案爲例。

+0

這是如何有效的實時?我的意思是,如果需求與OP所要求的完全相同,那麼就相比較而言。 –

+0

添加選項。 OP給出了一個非常無用的例子,所以我們不知道他真的想做什麼。顯然,爲一個決策創建一個數組並不是一條可行的路。但是班上的一些靜態結構很可能是這樣做的方式。 –

+0

明白了..謝謝.. –

1

此開關是更接近你的僞代碼,是準確的C#代碼:

int temp = /* 1, 2, or 3 */; 
string test; 
switch(temp) 
{ 
    case 1: 
     test = "yes"; 
     break; 
    case 2: 
     test = "no"; 
     break; 
    case 3: 
     test = "maybe"; 
     break; 
    default: 
     test = /* whatever you want as your default, if anything */; 
     break; 
} 

僞代碼不包括默認情況下,但它是很好的做法,包括一個。

0

您也可以扭轉乾坤:

class Program 
{ 
    enum MyEnum 
    { 
     Yes = 1, 
     No, 
     Maybe 
    } 

    static void Main(string[] args) 
    { 
     Console.WriteLine(MyEnum.Maybe.ToString()); 
     Console.ReadLine(); 
    } 
} 

這也比較符合該臨時只能是1,2或3。如果它是一個int,編譯器不會警告你,如果溫度得到的值34。

你也可以這樣做:

string GetText(int temp){ 
    return ((MyEnum)temp).ToString(); 
} 

的getText(2)將返回「否」