Why is there no exception when adding a null value to a string?

Why is this not an exception that does not understand, obj is null

object obj = null; Console.WriteLine("Hello World " + obj); 
+10
c # string-concatenation
Jul 22 '10 at 19:15
source share
5 answers

Compiles

 Console.WriteLine(String.Concat("Hello World ", obj)); 

String.Concat method ignores null parameters.

It is defined as follows: (From .Net link source)

  public static String Concat(Object arg0, Object arg1) { if (arg0==null) { arg0 = String.Empty; } if (arg1==null) { arg1 = String.Empty; } return Concat(arg0.ToString(), arg1.ToString()); } 

I do not know why it does not just return arg1.ToString() if arg0==null .

The String.Concat(string, string) method is defined as follows:

  public static String Concat(String str0, String str1) { if (IsNullOrEmpty(str0)) { if (IsNullOrEmpty(str1)) { return String.Empty; } return str1; } if (IsNullOrEmpty(str1)) { return str0; } int str0Length = str0.Length; String result = FastAllocateString(str0Length + str1.Length); FillStringChecked(result, 0, str0); FillStringChecked(result, str0Length, str1); return result; } 
+21
Jul 22 '10 at 19:16
source share
β€” -

Passing a null method to a method will not necessarily throw an exception; as for the implementation of the method (in which case, you will probably see ArgumentNullException ).

An attempt to access the member * of a null ** object is that it always throws a NullReferenceException guaranteed by ***.

So...

May or may not throw an exception

 object obj = null; SomeMethod(obj); // passing as parameter 

Defines an exception

 object obj = null; int hashCode = obj.GetHashCode(); // calling instance method 

In the case of the code in question, the parameter that you pass to Console.WriteLine is actually the result of a compiled call to string.Concat , which allows you to pass null values ​​as parameters and essentially ignores them - as SLaks already pointed out .




* Extension methods are another matter; they can be called by "zero" parameters; but since they are an illusion of action as instance methods, this rule does not apply to them. In fact, extension methods are, after all, only static methods. If you call a single value "on" a null , you effectively pass null as a parameter.

** Here I do not include Nullable<T> values ​​with HasValue == false ; although in many cases they are conveniently regarded as null , this is just for syntactic convenience: they are no more null than any other type of value can be null .

*** I am talking about C # here. As SLaks notes in a comment, this is not the rule of the CLI itself. But all calls to the instance method in C # are compiled into a callvirt statement in IL, which throws an exception if the instance is null .

+3
Jul 22 '10 at 19:20
source share

Because it will be annoying. For most pruposes, there is no semantic difference between an empty and an empty string.

+1
Jul 22 '10 at 19:21
source share

If you have a problem when this can happen in a real application, you can always check for null before displaying the text. Then you can display alternate text or nothing.

0
Jul 22 '10 at 19:19
source share

Because they implemented String.IsNullOrEmpty and left it to us to figure out how to use it.

0
Jul 22 '10 at 7:24 a.m.
source share



All Articles