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.

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 :-)

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345