Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get an exception when passing "null" constant but not when passing a "null" string reference?

If I run this code:

Console.WriteLine( String.Format( "{0}", null ) ); 

I get a ArgumentNullException but if I run this code:

String str = null; Console.WriteLine( String.Format( "{0}", str ) ); 

it runs just fine and the output is an empty string.

Now the two piece look equivalent to me - they both pass a null reference into String.Format() yet the behavior is different.

How id different behavior possible here?

like image 969
sharptooth Avatar asked Dec 14 '12 11:12

sharptooth


People also ask

How do I fix null reference exception in C#?

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.

How do you resolve null point exception?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What could be the reason if you are getting NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

Can null be passed as reference?

[Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.


1 Answers

Just decompile the code to work out what's going on.

string.Format("{0}", null) 

calls the most specific applicable overload, which is string.Format(string, object[]).

The overloads of string.Format are:

Format(String, Object) Format(String, Object[]) Format(IFormatProvider, String, Object[]) Format(String, Object, Object) Format(String, Object, Object, Object) 

Hopefully it's obvious why the last three options are invalid.

To work out which of the first two to use, the compiler compares the conversion from null to Object to the conversion from null to Object[]. The conversion to Object[] is deemed "better" because there's a conversion from Object[] to Object, but not vice versa. This is the same logic by which if we had:

Foo(String) Foo(Object) 

and called Foo(null), it would pick Foo(String).

So your original code is equivalent to:

object[] values = null; string.Format("{0}", values); 

At this point, hopefully you'd expect an ArgumentNullException - as per the documentation.

like image 86
Jon Skeet Avatar answered Oct 15 '22 04:10

Jon Skeet