Close this search box.

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.

This article is part of the GWB Archives. Original Author: Michael Freidgeim’s Blog

Related Posts