Snippet: String.Format Extension Method
Early this year Mitch suggested a string formatting syntax for C# vNext. His suggestion was that string.Format could be replaced with syntax like this:
throw new Exception( @("{0} {1} {2}"|a|b|c) );
I think it’s a great suggestion and I too would like to see it.
In the mean time, I’ve been making use of the following extension method:
throw new Exception("{0} {1} {2}".F(a, b, c));
I much prefer it to this:
throw new Exception(string.Format("{0} {1} {2}", a, b, c));
As not only is it shorter, but it removes the string.Format call from the front of the string, which I find easier to follow. The extension method is very simple:
public static class StringExtensions
{
public static string F(this string format, params object[] args)
{
return string.Format(format, args);
}
}
What do you think?
Filed under: C#


It’s better than Mitch’s abomination. (Sorry Mitch, but when you first posted that I thought it was some sort of mistimed April Fool’s Day joke. Talk about write-only code.)
The use of “F’ is a bit obtuse. I’d prefer if it was just spelled out as “Format”.
throw new Exception(”{0} {1} {2}”.Format(a, b, c));
… or how about “With”?
throw new Exception(”{0} {1} {2}”.With(a, b, c));
I agree with Matt. I’d like to combine both of his suggestions:
throw new Exception(â€{0} {1} {2}â€.FormatWith(a, b, c));
@Matt/@Thomas,
I like Format/FormatWith. It probably leads to less confusion than “F”.
Of course, it’s up to you - just copy the extension method, rename it and use it yourself
What about:
throw new Exception(”{0} {1} {2}” % {a, b, c});
you’d need to define the % operator for strings, and array’s right?
Yegh, you can’t extend operators yet.
My 2nd’ary fall-back syntax would be:
1. throw new Exception(_(”{0}, {1}, {2}”)(a, b, c));
2. throw new Exception(_(”{0}, {1}, {2}”, a, b, c));
3. throw new Exception(”{0}, {1}, {2}”._(a, b, c));
The first being the easiest to auto parse with all the gettext tools out there. The 2nd being a slightly shorter syntax. The 3rd being the easiest to implement as an extension method - much the same as using “F”.
Of course all your strings might be in a resources file, so you’re going to see:
throw new Exception(Resources.SillyUserException.F(a, b, c));
code for 1st syntax below:
public static class GetText
{
public delegate string FormatArgs(params object[] args);
public delegate FormatArgs FormatDelegate(string format);
public static FormatDelegate _
{
get
{
return x => y => String.Format(x, y);
}
}
}
the in code declare:
var _ = GetText._;
making it available throughout the whole class require a private static, and some tweaking, but still not ‘nice’.
Isn’t this some-what excessive and resulting in overhead that isn’t needed?
Sure it’d be pretty much invisible the performance hit, but if we start having extensions for every shortcut you can compound the hit.
1 x 1 second isn’t much
100 x 1 second is
(Obviously those aren’t realisitic numbers, just for examples sake)
I might be wrong, but I don’t think you can use Format as an extension method because it is a static method on String.
Aaron: so you mean I should do away with my “Plus” extension method?
int x = 2.Plus(3);
@Aaron - maybe, but I’d hope that the JIT compiler would optimize it out anyway. I shouldn’t have to worry about such things
@Eric - fortunately you can. If Format were an instance method on string, then maybe not (you still could, but the instance method takes priority over the extension method). Static methods on the class don’t conflict with instance methods, so they also don’t conflict with extension methods.
@Matt - I’ve been trying to work out if you’re being sarcastic or not? Unless your “Plus” method is doing something I can’t tell…
@Paul - yes, you’d hope that the compiler would optimize it, would be interesting to have a look at the IL to see though
Yeah Aaron, my Plus method does something you can’t tell:
public static int Plus(this int x, int y)
{
return x * y;
}
Out of interest:
@Matt/@Aaron, how did you guys know I’d left a reply?
@Matt - Your Plus method does a multiplication? Must be confusing
@Paul - Had the window open and hit refresh… What, I don’t have a boring life
So basically what you want is string.Format to be a member method and not a static method…
I could say this an extension method for extension method sake, but that is probably to harsh.
Just an opinion
Hi Ollie,
If I were building the .NET framework, yep, I’d probably want it as a member method. But despite my asking, Microsoft still haven’t given me write access to their TFS repository, so extension methods are my only solution
Paul
[…] extension methods can be abused, but I think this is another case where they makes the intent of the code clearer. That said, I have plenty of examples where […]
[…] I started stubbing out the Expect class, I decided to play with extension methods. This gives me two options - the classic Rhino.Mocks […]
I wasn’t able to use Format as an extension method to string. I got Error CS0176: Member ’string.Format(string, params object[])’ cannot be accessed with an instance reference; qualify it with a type name instead.
So, I changed the name to Fmt and it works fine.
Actually that should be less confusing since it is obviously a different method.