Remember a few posts ago when I was suggesting that we should be able to use named parameters in C# 4.0 in the same way that we use the “params” keyword right now? I was thinking that we could declare a method like this:

void Foo(params IDictionary<string, object> items);

… and then call it like this:

Foo(fizz: 1, buzz: "hello");

So our “items” dictionary now contains two key/value pairs: {“fizz”, 1} and {“buzz”, “hello”}.

I don’t know why it didn’t occur to me at the time, but I realise now that this syntax would be really handy for string formatting. Instead of the current String.Format method’s dependence on the order of the parameters, it could be keyword based. Imagine an extension method like this:

string Format(this string s, string format, params IDictionary<string, object> args);

Now we can format our strings using this syntax:

return String.Format("{name} - {dob}", name: "Matt", dob: new DateTime(1973, 3, 22));
Isn’t that a bit more intuitive than the “{0} - {1}” format strings you’re used to? I think a “named params” feature like this would be really handy.