C# 使用 String.IsNullOrEmpty 切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Switch with String.IsNullOrEmpty
提问by maxfridbe
Is it possible to have a switch in C# which checks if the value is null or empty not "" but String.Empty
? I know i can do this:
是否可以在 C# 中有一个开关来检查值是否为空或空而不是“” String.Empty
?我知道我可以这样做:
switch (text)
{
case null:
case "":
break;
}
Is there something better, because I don't want to have a large list of IF statements?
有没有更好的东西,因为我不想有大量的 IF 语句?
I'mm trying to replace:
我正在尝试替换:
if (String.IsNullOrEmpty(text))
blah;
else if (text = "hi")
blah
采纳答案by Maxime Rouiller
I would suggest something like the following:
我建议如下:
switch(text ?? String.Empty)
{
case "":
break;
case "hi":
break;
}
Is that what you are looking for?
这就是你要找的吗?
回答by JoshBerke
how about
怎么样
if (string.isNullOrEmpty(text))
{
//blah
}
else
{
switch (text)
{
case "hi":
}
}
}
回答by recursive
From the documentationof String.Empty
:
从文件中String.Empty
:
The value of this field is the zero-length string, "".
该字段的值是零长度字符串“”。
I interpret this to mean that there is no difference between ""
and String.Empty
. Why are you trying to distinguish between them?
我将此解释为""
和之间没有区别String.Empty
。你为什么要区分它们?
回答by Michael Burr
What's wrong with your example switch
statement?
你的示例switch
语句有什么问题?
switch (text)
{
case null:
case "":
foo();
break;
case "hi":
bar();
break;
}
It works (and for some reason that surprised me - I thought it would complain or crash on the null
case) and it's clear.
它有效(并且出于某种原因让我感到惊讶 - 我认为它会抱怨或崩溃null
)并且很明显。
For that matter, why are you worried about String.Empty
? I'm missing something here.
为此,你为什么担心String.Empty
?我在这里遗漏了一些东西。
回答by Jeroen Landheer
An empty string is "", which is equal to String.Empty. The reason that you canput "" in a case statement but not "String.Empty" is that "Empty" is a field of the class "String" and "" is actually a contant value.
空字符串是 "",它等于 String.Empty。您可以将 "" 放在 case 语句中而不是 "String.Empty" 的原因是 "Empty" 是类 "String" 的字段,而 "" 实际上是一个常量值。
Constant values are allowed in cases, String.Empty is a field and could be altered at run time. (In this case it will remain the same, but not all static fields of each class are constant values.)
在某些情况下允许使用常量值,String.Empty 是一个字段,可以在运行时更改。(在这种情况下,它将保持不变,但并非每个类的所有静态字段都是常量值。)
In the case of 'if', that condition is evaluated at run time and if does notrequire a constant value.
在“如果”的情况下,该条件是在运行时评估,并且如果它不要求恒定值。
I hope this explains why.
我希望这能解释原因。
回答by Even Mien
Something that I just noticed is that you can combine if/else and switch statements!Very useful when needing to check preconditions.
我刚刚注意到的是,您可以结合 if/else 和 switch 语句!在需要检查前提条件时非常有用。
if (string.IsNullOrEmpty(text))
{
//blah
}
else switch (text)
{
case "hi":
Console.WriteLine("How about a nice game of chess?");
break;
default:
break;
}
回答by sree ranjith c.k
string StrMode;
if (!string.IsNullOrEmpty(StrMode))
{
switch (StrMode.Trim())
{
case "Souse":
{
//Statement Eg:
MesssageBox.Show("Souse");
break;
}
case "Company Agent":
{
//Statement Eg:
MesssageBox.Show("Souse");
break;
}
default:
return;
}
}