Is it valid to use `<%= "{0}, {1}", arg1, arg2 %>` in place of `<%= string.Format("{0}, {1}", arg1, arg2) %>` in ASP.NET aspx pages
Asked Answered
P

1

6

In my aspx pages I often use the following and it works fine:

<%= "{0}, {1}", arg1, arg2 %>

I use ReSharper for code analysis. I just upgraded for v6.1 to 7 and it is giving me the following two errors:

"Expression expected"

"Method '__ReSharper_Render' has 1 parameter(s) but is invoked with 3 argument(s)"

Is the syntax I use incorrect? I would prefer to continue using it as I find it quite elegant and compact. If it is correct (I think it should be as it works), any idea how to tell ReSharper to either ignore it or treat it as valid?

Pincenez answered 11/4, 2013 at 10:47 Comment(2)
Why even use string formatting? isn't this equivalent to <%=arg1%>, <%=arg2 %>?Leptophyllous
Not really equivalent. Depending on the string format and the number of arguments it might not be very readable. And the IDE might reformat it.Pincenez
M
4

Saurabh, you are using implementation details of ASP.NET. It's bad practice. Better to specify it explicitly:

<%= string.Format("{0}, {1}", arg1, arg2) %>
Minnich answered 11/4, 2013 at 12:28 Comment(2)
Thanks for the answer. Actually it was ReSharper 6.1 that suggested the compact format. And the newer version doesn't and shows it as an error. Just for my knowledge, can you explain what you mean by "implementation details"?Pincenez
ASP.NET generate code like this for your example: System.Web.UI.HtmlTextWriter @w; w.Write("{0}, {1}", arg1, arg2) If, for example, in future they assign it fist to variable - your code will be broken: System.Web.UI.HtmlTextWriter @w; object tmp = "{0}, {1}", arg1, arg2; w.Write(tmp);Minnich

© 2022 - 2024 — McMap. All rights reserved.