Does .NET have a way to allow an empty string for String.Empty?

I have a single line method that resolves an empty string to string.Empty, which I thought might be useful as an extension method, but I cannot find a useful way to do this.

The only way I could see that this is useful is with the static method in the string class, because obviously it cannot be attributed to the instance since the instance is null and this causes a compiler error. [Edit: A compiler error occurred due to an uninitialized variable that I misinterpreted]

I was thinking of adding it to the helper class, but that just adds unnecessary complexity in terms of openness.

So this question has two parts:

  • Is there a built-in way in the .NET Framework to allow an empty string for string.Empty, which is well known that I missed somewhere along the way?
  • If this is not the case, does anyone know a way to add this method as a static extension of the string class?

Greetings in advance

Edit:

Well, I think I should have been a little clearer - I already know the zero treat well, and I used it in the place where I have a dozen lines that are checked to calculate the hash code.

As you can imagine, 12 lines of code that are closely related to each other, all that contains zero-coalescent syntax, are an eyesore, so I moved the zero-collision operation to a method to make it easier on the eyes. However, this is an ideal extension for a string object:

int hashcode =
FirstValue.ResolveNull().GetHashCode() ^
SecondValue.ResolveNull().GetHashCode() ^
...

more than a dozen lines are much easier to read than:

int hashcode =
(FirstValue ?? String.Empty).GetHashCode() ^
(SecondValue ?? String.Empty).GetHashCode() ^
...

, null, :

 string s;

, , :

string s = null;

:

s.ResolveNull();

.

+5
10

, , , - , , , , null, .

# 3.0 ( ), . .

+6

, - . , , coalesce:

string s = null;
string x = s ?? string.Empty;
+24

someExpressionInvolving (s?? "");

string.Empty vs "" - :

Console.WriteLine(ReferenceEquals(string.Empty, "")); // true
+4

:

public static class StringExtensions
{
    public static String EmptyIfNull(this String instance)
    {
        return instance ?? String.Empty;
    }
}

, instance ?? String.Empty , String.Empty null.

+3
string test = CouldReturnNull() ?? string.Empty;
+1

?? ( ) # - , ? ?

.

string n = null;
string y = n ?? String.Empty;

y = String.Empty;

+1
string abc = s ?? string.Empty;
+1

string.Empty , null

string a = null;
string b = null + string.Empty;
string c = "a" + string.Empty;

(a == string.Empty).Dump(); // False
(b == string.Empty).Dump(); // True
(c == "a").Dump(); // True

LinqPad, ..Dump()

...

string b = null + string.Empty;

IL_0001:  ldsfld      System.String.Empty
IL_0006:  dup         
IL_0007:  brtrue.s    IL_000F
IL_0009:  pop         
IL_000A:  ldstr       ""
IL_000F:  stloc.0     

:

string b = null ?? string.empty

IL_0001:  ldsfld      System.String.Empty
IL_0006:  stloc.0     

string b = null + "";

IL_0001:  ldstr       ""
IL_0006:  stloc.0     

-

string b = null + "";

, IMO, ,

0

, , " , ". , , string.Empty:

public void SomeMethod(string param)
{
    string notNull = param + string.Empty;
}

, , , , .

0

There is no built-in function for this. But you can write a small extension method (or static mantode) for a string type with an expression like:

return s ?? string.Emtpy;

0
source

All Articles