String.IsNullOrEmpty (myString) Vs myString! = Null

Which one is better among the three?

string myString = ""; 
String.IsNullOrEmpty(myString);

vs

string myString = "";
if(myString.Length > 0 || myString != null)

vs 

string myString = "";
if (m.Length > 0 | m != null)

Faster clearer, but is there a difference in performance between the two? What if the string is never empty, for example, if it is taken from a text field that may be empty but not empty?

+5
source share
7 answers

Well, the version in question:

if(myString.Length > 0 || myString != null)

, null ( ) - null, .Length. string.IsNullOrEmpty. , , ( null).

static bool HasValue(this string s) {
    return !string.IsNullOrEmpty(s);
}
+15

string.IsNullOrEmpty(str). . .

"", string.Empty, .

+4

IsNullOrEmpty.

, .

- . , : " , myString.trim(). Length!= 0 " .

: - , , .

+2

, IsNullOrEmpty() JIT (. ).

, - , .NET, .NET 4:

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}

, NGen ( native).

+2

String.IsNullOrEmpty - , , ( , , , ...;).

IsNullOrEmpty:

if (String.IsNullOrEmpty(s)) ...

:

if (s == null || s.Length == 0) ...

, , :

if (s.Length == 0) ...

IsNullOrEmpty , , - , , IsNullOrEmpty , , , .

+1

, String.IsNullOrEmpty(String s) :

if (s == null || s.Length == 0)...

API.

0

I believe the String.IsNullOrEmpty(String s) is implemented as: if (s == null || s.Length == 0) ... in the API.

. , , . s NULL, s.Length .

-2
source

All Articles