Passing null parameters to String.Format is safe

I wanted to use String.Format with parameters that can be null. Firstly i decided to put some conditional code like
if(arg1!=null) but then desided to check, if it is handled automatically. And from Reflector it looks that it handles nulls

if (str2 == null)
    {
        str2 = string.Empty;
    }
So it safe to write code like 

String.Format("({0}{1})", a1,a2 ) ,

even if some parameters are null.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Wednesday, January 09, 2008 3:54 PM
Print

Comments on this entry:

# re: Passing null parameters to String.Format is safe

Left by web development company at 8/20/2009 9:04 AM
Gravatar
That was inspiring,

I thought that it would true an exception when you pass a null parameter to it!

Keep up the good work

# Never was sure

Left by Uwe at 11/2/2009 3:23 AM
Gravatar
Did this from time to time but never was sure.

You clarified. Thanks :-)

# re: Passing null parameters to String.Format is safe

Left by oddbear at 11/3/2010 3:58 AM
Gravatar
What ain't safe, is to pass null objects with strings "inside" them.

Example:
Person person;
string.Format("Test {0}", person.age);

will crash, but:
Person person = new Person();
string.Format("Test {0}", person.age);
will not crash.

# re: Passing null parameters to String.Format is safe

Left by e at 3/31/2011 9:02 AM
Gravatar
oddbear, your example has nothing to do with String.Format. It fails even *before* the parameters are passed, when "person.age" is evaluated. You seem to be confusing objects with object-references.

# re: Passing null parameters to String.Format is safe

Left by Marty at 8/22/2011 10:46 AM
Gravatar
it can't be a single null literal or a object array though.
//throws
string.Format("{0}", null);

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

//passes
string.Format("{0}", (string)null);

//passes
MyClass mc = null;
string.Format("{0}", mc);

//passes
string.Format("{0}{1}", null, null);

basically, you just want to avoid calling the
public static string Format(string format, params object[] args) overload because it checks args for null while all the other overloads do not.

This is all tested with v4 of mscorlib
just fyi

# re: Passing null parameters to String.Format is safe

Left by Andrei at 10/4/2011 4:28 AM
Gravatar
You shouldn't reflect the .net code to find out how it works, it should be in the spec. For string.Format it is the case http://msdn.microsoft.com/en-us/library/aa331875%28VS.71%29.aspx ("A zero-based integer that indicates which element in a list of objects to format. If the object specified by index is a null reference (Nothing in Visual Basic), then the format item is replaced by the empty string ("")"). And if an implementation detail is not in the spec, better don't rely on it in your code

# re: Passing null parameters to String.Format is safe

Left by Michael Freidgeim at 10/5/2011 2:35 AM
Gravatar
Andrei,
Unfortunately, the documentation is not always correct or comprehensive. In particular, you pointed to comments regarding the index in format, not the description of arguments.
I've added it to MS community content

# re: Passing null parameters to String.Format is safe

Left by Andrei at 10/6/2011 4:38 AM
Gravatar
Actually I pointed to comments regarding precisely the behaviour you are describing in your post. Just read the spec more carefully :).

This rule of thumb to never assume implementation details which are not in the spec has never put me in trouble, on the contrary.

But nevermind, it's really not very important.

Thank you for your post btw (I read it before reading the spec, while I was googling for the answer to this question, so it was useful to me) !

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910