Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is this ASP.NET feature documented? <%= string format, params object[] args %>

Apparently it is possible to write formatted output using the <%= %> construct (render block) in ASP.NET web forms pages and views.

<%= "{0} is {1}", "Foo", 42 %>

This will render "Foo is 42". As far as I know the ASP.NET parser translates <%= %> into a call to HttpResponse.Write(string). Obviously in the code above, there is no one-to-one translation, because the number of arguments don't match (assuming the , in the expression above separates arguments).

Now I have seen that the class TextWriter has a Write(string, object[]) method.

I have checked the output from the parser, and indeed it calls the TextWriter's method that accepts a params object[] argument for formatting:

private void @__Renderform1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
    // ...
    @__w.Write( "{0} is {1}", "Foo", 42 );

Is that behavior documented anywhere?

like image 975
Michiel van Oosterhout Avatar asked Sep 06 '13 11:09

Michiel van Oosterhout


People also ask

What is string format?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.

What is .NET format?

. NET defines a set of standard format specifiers for all numeric types, all date and time types, and all enumeration types. For example, each of these categories supports a "G" standard format specifier, which defines a general string representation of a value of that type.

What is Composite formatting in C#?

The composite formatting feature returns a new result string where each format item is replaced by the string representation of the corresponding object in the list. Consider the following Format code fragment: C# Copy. string name = "Fred"; String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);


1 Answers

As far as I know the ASP.NET parser translates <%= %> into a call to HttpResponse.Write(string).

Maybe the <%= "{0} is {1}", "Foo", 42 %> is translated to Response.Output.Write(string format, params object[] arg), Output being of type TextWriter, which would be the explanation according to http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx

like image 104
jbl Avatar answered Oct 03 '22 22:10

jbl