oop*_*ase 198
if (string.IsNullOrEmpty(myString)) {
//
}
Run Code Online (Sandbox Code Playgroud)
mad*_*dd0 42
从.NET 2.0开始,您可以使用:
// Indicates whether the specified string is null or an Empty string.
string.IsNullOrEmpty(string value);
Run Code Online (Sandbox Code Playgroud)
此外,自.NET 4.0以来,有一种新方法更进一步:
// Indicates whether a specified string is null, empty, or consists only of white-space characters.
string.IsNullOrWhiteSpace(string value);
Run Code Online (Sandbox Code Playgroud)
如果变量是一个字符串
bool result = string.IsNullOrEmpty(variableToTest);
Run Code Online (Sandbox Code Playgroud)
如果你只有一个可能包含或不包含字符串的对象
bool result = string.IsNullOrEmpty(variableToTest as string);
Run Code Online (Sandbox Code Playgroud)