Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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.

Posted on Wednesday, January 9, 2008 3:54 PM .Net Framework | Back to top


Comments on this post: Passing null parameters to String.Format is safe

# re: Passing null parameters to String.Format is safe
Requesting 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
Left by web development company on Aug 20, 2009 9:04 AM

# Never was sure
Requesting Gravatar...
Did this from time to time but never was sure.

You clarified. Thanks :-)
Left by Uwe on Nov 02, 2009 3:23 AM

# re: Passing null parameters to String.Format is safe
Requesting 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.
Left by oddbear on Nov 03, 2010 3:58 AM

# re: Passing null parameters to String.Format is safe
Requesting 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.
Left by e on Mar 31, 2011 9:02 AM

# re: Passing null parameters to String.Format is safe
Requesting 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
Left by Marty on Aug 22, 2011 10:46 AM

# re: Passing null parameters to String.Format is safe
Requesting 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
Left by Andrei on Oct 04, 2011 4:28 AM

# re: Passing null parameters to String.Format is safe
Requesting 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
Left by Michael Freidgeim on Oct 05, 2011 2:35 AM

# re: Passing null parameters to String.Format is safe
Requesting 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) !
Left by Andrei on Oct 06, 2011 4:38 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net