String str
1:string. IsNullOrEmpty = = Str This method is my favorite, it not only can judge "empty string variable", but also can judge "value is empty string variable", and also can make the code simple and beautiful. The efficiency of judgment is also not low.
2:str. Length = = 0 This way, I do not like to use, not recommended. Looking at the Internet and your own actual tests can prove that this is the most efficient way to do this, but to use it you have to make sure that the string is not NULL, and if it is null, the exception is reported.
3.str = = String. Empty or str = = "" These two ways, I also do not recommend to use, he can only judge "value is empty string" of the string variable, and efficiency is low
4.STR = = NULL This way I am not recommended, reason and 31 kind.
The following is from Microsoft Official website:
String.IsNullOrEmpty method
indicates that the specified string is null or isEmptystring.
public static bool IsNullOrEmpty (string value)
Parameters
return value
Type: System. Boolean
trueif the value parameter is null or an empty string (""); otherwise false.
isnullorempty is an easy way to test whether a String is null at the same time or whether its value is empty. it is equivalent to the following code:
result = S = = NULL | | s = = String.Empty;
can useIsnullorwhitespacemethod to test whether a string null , whose value isString. Empty, or contains only white space characters.
Using system;class sample { public static void main () { string s1 = "ABCD"; string s2 = ""; string s3 = null; console.writeline ("string s1 {0}.", test (S1)); console.writeline (" String s2 {0}. ", test (S2)); console.writeline (" string s3 {0}. " , test (S3)); } public static string test ( String s) { if (String.IsNullOrEmpty (s)) return "Is null or empty"; else return string.format ("(\" {0}\ ") is Neither null nor&nBsp;empty ", s); }}
C # string null (IsNullOrEmpty)