Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which methods in the 3.5 framework have a String.Format-like signature?

I just noticed that I can do the following, which came as a complete surprise to me:

Console.WriteLine("{0}:{1}:{2}", "foo", "bar", "baz");

This works for the Write method too. What other methods have signatures supporting this, without requiring the use of String.Format?

Debug.WriteLine doesn't...
HttpResponse.WriteLine doesn't...

(And on a side note, I could not find a quick way of searching for this with Reflector. What is a good way to search for specific signatures?)

Edit:

Specifically for the 3.5 framework.

like image 469
D'Arcy Rittich Avatar asked Feb 16 '10 16:02

D'Arcy Rittich


People also ask

What is use of string format in C#?

In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.

What is the meaning of 0 in c#?

CSharp Online Training The “0” custom specifier is a zero placeholder. If the value to be formatted has a digit in the position where the zero appears in the format string, the the digit is copied to the resultant string.


2 Answers

There are a lot of methods that support this throughout the framework. All subclasses of TextWriter (and therefore StreamWriter and StringWriter and their subclasses) will inherit the Write method that supports this.

Another example that is often used is StringBuilder.AppendFormat.

You can write your own methods to support this too. You can do it by having an overload with a format string parameter and another parameter with the params keyword, like this:

public void Foo(string message) {
    // whatever
}

public void Foo(string format, params string[] arg) {
    Foo(string.Format(format, arg));
}
like image 198
Mark Byers Avatar answered Sep 30 '22 17:09

Mark Byers


StringBuilder instances have an AppendFormat method.

StringWriter instances have a Write overload which takes format parameters.

like image 21
John Hoven Avatar answered Sep 30 '22 17:09

John Hoven