How does string.Format handle null values?

In the following code, why are two calls to string.Format not behaving the same? In the first case, an exception is not thrown, and in the second, an ArgumentNullException is thrown.

 static void Main(string[] args) { Exception e = null; string msgOne = string.Format("An exception occurred: {0}", e); string msgTwo = string.Format("Another exception occurred: {0}", null); } 

Can someone please help me understand the difference between the two?

+61
c # string.format
Nov 07
source share
4 answers

I guess here, but it looks like the difference is that you are pressing an overloaded call. String.Format has several overloads, this is what you click on.

In the first example, it would be reasonable that you click String.Format(string,object) .

In the second example, providing null , you will most likely hit String.Format(string,params object[]) , which in the documentation will raise an ArgumentNullException when:

Format

or args is null.

If you are using .NET4, try using named parameters:

 String.Format("Another exception occured: {0}", arg0: null); 

Why does this happen when params object[] overloaded? Probably because null not an object, and the params way works in that you can either pass each value as a new object in the call, or pass an array of values ​​to it. In other words, one in the same :

 String.Format("Hello, {0}! Today is {1}.", "World", "Sunny"); String.Format("Hello, {0}! Today is {1}.", new Object[]{ "World", "Sunny" }) 

Thus, it transfers the call of your operator to something line by line:

 String format = "Another exception occured: {0}"; Object[] args = null; String.Format(format, args); // throw new ArgumentNullException(); 
+49
Nov 07 '12 at 19:11
source share

In the first example, you click Format(String, Object) , which looks at the same time parsed:

  public static string Format(string format, object arg0) { return Format(null, format, new object[] { arg0 }); } 

Note the new object[] around this.

Secondly, you are apparently clicking on using Format(string, object[]) , at least the one that gets called when I run the same test. Parse, it looks like this:

  public static string Format(string format, params object[] args) { return Format(null, format, args); } 

So, all of them are actually passed to Format(IFormatProvider, string, object[]) . Cool, let's look at the first few lines:

 public static string Format(IFormatProvider provider, string format, params object[] args) { if ((format == null) || (args == null)) { throw new ArgumentNullException((format == null) ? "format" : "args"); } ... } 

... welp, here is your problem, right now! The first call is to transfer it to a new array, so it is not equal to zero. Passing to null does not explicitly do this, due to the specific instance of Format() calling.

+12
Nov 07 '12 at 19:12
source share

The first call is resolved as a call to Format (object), and the second is defined as a call to Format (object []). The Null parameter is handled differently by these different overloads.

Overload description is described here . The relevant part is that for the second call to Format, the format overload (params object []) expands to Format (object []), which is preferable to Format (object). The literal value null is both an object [] and an object, but the object [] is more specific, so it is selected.

+2
Nov 07 '12 at 19:14
source share

There are two differences:

  • Null is assigned here.

     Exception e = null; string msgOne = string.Format("An exception occurred: {0}", e); 
  • Here, the Null value cannot be read in string format, which means an error occurred while typing.

     string msgTwo = string.Format("Another exception occurred: {0}", null); 

I will give you a simple example: Here you cannot read the NULL value as a string format.

 string str = null.toString(); 
-3
Nov 08 '12 at 18:40
source share



All Articles