String.Format and IFormatProvider
Spotted some odd behaviour with String.Format when there is more than one parameter and you are supplying a format string as a variable that could potentially be null.
For instance, I have:
private object FooBar()
{
return null;
}
private void SomeMethod()
{
var foo = FooBar() as string;
var bar = String.Format(foo, "foo", "bar");
}
ReSharper offers to 'Check if 'Foo' is not null'
When I choose this option, I get:
private void SomeMethod()
{
var foo = FooBar() as string;
if (foo != null)
{
var bar = String.Format((IFormatProvider) foo, "foo", "bar");
}
}
which clearly isn't right.
whereas if there is only a single parameter then resharper does it properly:
private void SomeMethod()
{
var foo = FooBar() as string;
if (foo != null)
{
var bar = String.Format(foo, "foo");
}
}
Please sign in to leave a comment.