views:

133

answers:

5

I often use String.Format() because it makes the building of strings more readable and manageable.

Is there anyway to reduce its syntactical verbosity, e.g. with an extension method, etc.?

Logger.LogEntry(String.Format("text '{0}' registered", pair.IdCode));

public static void LogEntry(string message)
{
    ...
}

e.g. I would like to use all my and other methods that receive a string the way I use Console.Write(), e.g.:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
+1  A: 

If Logger.LogEntry is a static method outside of your control, then no; you can only add extension methods to instances. If it is your type, you could add:

public static void LogEntry(string format, params object[] args) {
    ... string.Format(format,args) ...
}
Marc Gravell
+4  A: 

If you control the Logger.LogEntry method, you can simply add an overload that encompasses the string.format. Just declare the second parameter as a paramarray and you are good to go!

Mitchel Sellers
@Mitchel: I think that's a reasonable answer in this case, but I'd suggest that an extension on String is more generally useful.
Steven Sudit
@Steven - Yes and no, for his project yes, for other callers of Logger.LogEntry() an overload would be better
Mitchel Sellers
+12  A: 

How about:

static void LogEntry(string format, params object[] args) {
    Console.WriteLine(format, args); // For example.
}

Now you can call it like this:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
Konrad Rudolph
@Konrad param gives me a compiler error. The keyword is params.
chocojosh
@chocojosh: Typo. Thanks.
Konrad Rudolph
+2  A: 

Yes, you can make an extension method named FormatWith, which lets you say things like:

Logger.LogEntry("I hate my {0}".FormatWith(itemName));

It should be easy enough to roll your own, but here's an example: http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx

Steven Sudit
Great point, but from a developer perspective, Logger.LogEntry is a utility function that many people are going to call, it would be good, in my opinion to get the method to support it as well. Plus that is just about as verbose as string.format..... But to the authors point it is the most correct answer.
Mitchel Sellers
I don't disagree regarding the usefulness of the overload you suggested: I've done much the same myself. I do disagree with the idea that a FormatWith method is as verbose as String.Format, not only because it's shorter but because having it (appear) as a non-static method makes it much less obtrusive. I believe the real question is whether an ad hoc overload is a general solution. You're not going to make an overload for every method that takes a string, but with an extension method, you get the benefit of doing so.
Steven Sudit
@Mitchel Sellers: Sorry, forgot to use an @you to make sure you saw the response.
Steven Sudit
A: 

You could use the params keyword to combine all of the arguments after the first argument into an array and pass that array to String.Format.

static void FormatString(string myString, params string[] format)
{
     Console.WriteLine(String.Format(myString, format));
}
chocojosh