How to add empty string in C #?

I was surprised to see an example of a line initialized to zero, and then adding something to it in a production environment. He just cried wrong.

I was sure that this would throw an exception from the null object, but this very shorthand example also works:

string sample = null; sample += "test"; // sample equals "test" 

* Please note that the source code that I found sets the string property to null and is added to it elsewhere, so the answers related to the compiler optimizing zero at compile time do not matter.

Can someone explain why this works without errors?

Subsequent:

Based on Leppie's answer, I used Reflector to see what's inside the string. Concat. Now it’s really obvious why this transformation takes place (there is no magic at all):

 public static string Concat(string str0, string str1) { if (IsNullOrEmpty(str0)) { if (IsNullOrEmpty(str1)) { return Empty; } return str1; } if (IsNullOrEmpty(str1)) { return str0; } int length = str0.Length; string dest = FastAllocateString(length + str1.Length); FillStringChecked(dest, 0, str0); FillStringChecked(dest, length, str1); return dest; } 

** Note: the specific implementation I studied (in the .Net library from Microsoft) does not convert to blank lines, as suggested by C # standards and most answers, but uses several tests to shorten the process, the end result is the same as if if he were, but there you go :)

+43
string null c #
Oct. 31 '11 at 12:10
source share
4 answers

The + operator for strings is only a shorthand for string.Concat , which simply turns null arguments into empty strings before concatenation.

Update:

The generalized version of string.Concat:

 public static string Concat(params string[] values) { int num = 0; if (values == null) { throw new ArgumentNullException("values"); } string[] array = new string[values.Length]; for (int i = 0; i < values.Length; i++) { string text = values[i]; array[i] = ((text == null) ? string.Empty : text); num += array[i].Length; if (num < 0) { throw new OutOfMemoryException(); } } return string.ConcatArray(array, num); } 
+45
Oct. 31 '11 at 12:13
source share
— -

The corresponding citation should be ECMA-334 §14.7.4:

String concatenation:

 string operator +(string x, string y); string operator +(string x, object y); string operator +(object x, string y); 

The binary + operator concatenates strings when one or both operands are of type string . If the string operand is null , the empty string is replaced. Otherwise, any operand without a string is converted to its string representation by calling the virtual ToString method, inherited from the object type. If ToString returns null , the empty string is replaced.

+6
Oct. 31 '11 at 17:51
source share

it's because

In string concatenation operations, the C # compiler processes the null value of a string the same as an empty string, but it does not convert the value of the original empty string.

From How to Combine Multiple Lines (C # Programming Guide)

The binary + operator concatenates strings when one or both operands are of type string. If the string concatenation operand is null, the empty string is replaced. Otherwise, any string argument is converted to its string representation, by invoking the ToString virtual method inherited from the type object. If ToString returns null, the empty string is replaced.

From Add operator

+2
Oct 31 '11 at 12:15
source share

Here is what your code compiles for

 string sample = null; sample += "test"; 

compiled into this IL code:

 .entrypoint // Code size 16 (0x10) .maxstack 2 .locals init ([0] string sample) IL_0000: nop IL_0001: ldnull IL_0002: stloc.0 IL_0003: ldloc.0 IL_0004: ldstr "test" IL_0009: call string [mscorlib]System.String::Concat(string, string) IL_000e: stloc.0 IL_000f: ret 

And String.Concat takes a NULL string.

+1
Oct 31 '11 at 12:19
source share



All Articles