可能重複:
Differences in string compare methods in C#如何以正確的方式將字符串與文本進行比較?
我知道,在C#字符串對象,比較兩個字符串時,我剛剛瞭解CompareTo方法。但是如果我需要將字符串與特定文本值進行比較呢?
現在我這樣做:
private string choice;
[...some code...]
if (choice == "1")
{
Console.WriteLine("You choose 1");
Console.ReadLine();
}
else if (choice == "2")
{
Console.WriteLine("You choose 2");
Console.ReadLine();
}
etc...
和它的作品只是因爲我想要的。但它是好的編碼嗎?無法找到關於此特定主題的任何信息。
考慮使用'之開關語句。 –
你可能想在第一種情況下輸出「你選擇了1」:) –
thx符文。 Metro Smurf:我看到了這個問題,但我的情況並沒有出現在這個話題中。 – Christoffer