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.
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.
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.
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));
}
StringBuilder instances have an AppendFormat method.
StringWriter instances have a Write overload which takes format parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With