Skip to content

Commit

Permalink
[System] handle arguments whose ToString returns null in String.Format
Browse files Browse the repository at this point in the history
When an argument to String.Format has no formatter, its ToString method
is called. This change takes care that the FormatHelper can handle
arguments whose ToString() method returns null.
  • Loading branch information
DavidS committed Apr 16, 2014
1 parent 945faf6 commit 3f9dd10
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mcs/class/corlib/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@ internal static StringBuilder FormatHelper (StringBuilder result, IFormatProvide
if (arg is IFormattable)
str = ((IFormattable)arg).ToString (arg_format, provider);
else
str = arg.ToString ();
str = arg.ToString () ?? Empty;
}

// pad formatted string and append to result
Expand Down
14 changes: 14 additions & 0 deletions mcs/class/corlib/Test/System/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public object GetFormat (Type formatType)
}
}

class ToNullStringer
{
public override string ToString()
{
return null;
}
}

private CultureInfo orgCulture;

Expand Down Expand Up @@ -1209,6 +1216,13 @@ public void Format ()
Assert.AreEqual ("test", s);
}

[Test]
public void Format_Arg_ToNullStringer ()
{
var s = String.Format ("{0}", new ToNullStringer ());
Assert.AreEqual ("", s);
}

[Test]
public void TestGetEnumerator ()
{
Expand Down

0 comments on commit 3f9dd10

Please sign in to comment.