Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format with null format

Can anyone explain why the following occurs:

String.Format(null, "foo") // Returns foo
String.Format((string)null, "foo") // Throws ArgumentNullException:
                                   // Value cannot be null. 
                                   // Parameter name: format

Thanks.

like image 949
JKJKJK Avatar asked Dec 21 '22 22:12

JKJKJK


1 Answers

Its calling a different overload.

string.Format(null, "");  
//calls 
public static string Format(IFormatProvider provider, string format, params object[] args);

MSDN Method Link describing above.

string.Format((string)null, "");
//Calls (and this one throws ArgumentException)
public static string Format(string format, object arg0);

MSDN Method Link describing above.

like image 151
Nix Avatar answered Jan 01 '23 05:01

Nix