the question is really simple. How do we format strings in C#? this way:
string.Format("string goes here with placeholders like {0} {1}", firstName, lastName);
Now, is it possible to create an extension method to do it this way?
"string goes here {0} {1}".Format(firstName, lastName);
That’s all.
Well, it’s more complicated than it looks. Others say this is possible, and I don’t doubt them, but it doesn’t seem to be the case in Mono.
There, the standard overloads of the
Format()method seem to take precedence in the name resolution process, and compilation fails because a static method ends up being called on an object instance, which is illegal.Given this code:
The Mono compiler (mcs 2.10.2.0) replies with:
Of course, the above compiles cleanly if the extension method is not named
Format(), but maybe you actually want to use that name. If that’s the case, then it’s not possible, or at least not on all implementations of the .NET platform.