开发者

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

开发者 https://www.devze.com 2022-12-20 03:10 出处:网络
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\");

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 signatur开发者_如何学Pythones 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.


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.


Debug.WriteLine(string, params Object[] args) overload does this as well, being added in .Net 4.0.


The StringBuilder class has a method called AppendFormat which behaves in the same way

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号