Difference between anonymous class and IDictionary for htmlAttributes in ASP.NET MVC?

There's not too much difference, however using the anonymous object is a cleaner and more readable syntax for the caller, and now considered more standard practice. There might be a slight, negligible performance benefit to using IDictionary if you're a fan of micro-optimisation.

Up vote 3 down vote favorite share g+ share fb share tw.

For example if you check these two extension methods the only difference is type of htmlAttributes so you can pass your htmlAttributes in two different ways: public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression> expression, IDictionary htmlAttributes); public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes); And use them in either of these ways: @Html. TextBoxFor(model => model. TagLine, new { @placeholder = "We live to make art.

" }) @Html. TextBoxFor(model => model. TagLine, new Dictionary { { "placeholder", "We live to make art.

" } }) I have checked MVC source code and I know in the background they use same method, but the one which accepts the anonymous object uses HtmlHelper. AnonymousObjectToHtmlAttributes(htmlAttributes) to make the anonymous object a dictionary. In my point of view, views are cleaner to use anonymous object.

What do you think guys? Are there any drawbacks to using an anonymous object? C# asp.

Net-mvc asp. Net-mvc-3 mvc link|improve this question edited Nov 17 '11 at 0:12Chris Fulstow12.4k935 asked Nov 16 '11 at 23:36Azadeh Khojandi584.

There's not too much difference, however using the anonymous object is a cleaner and more readable syntax for the caller, and now considered more standard practice. There might be a slight, negligible performance benefit to using IDictionary if you're a fan of micro-optimisation. The IDictionary overload option has probably stuck since ASP.

NET MVC 1.0 CTP days when C# 3.0 and anonymous objects were still quite new. Eilon Lipton's blog post proposing Using C# 3.0 Anonymous Types as Dictionaries gives some background.

The dictionary overloads are useful in some cases where you already have a dictionary. – Haacked Nov 21 '11 at 18:42.

Chris answers all the thing. I give 1 more reason why use IDictionary: You can't not use anonymouse object when you need a HTML5 attribute like "data-something" :D Cheers.

1 You can use this with underscore , like : @Html. TextBoxFor(model => model. TagLine, new { data_as_multiple = "true" }) – Azadeh Khojandi Nov 17 '11 at 0:17 1 From MVC 3.0 onwards you can use an underscore, e.g. New { data_something = "value" }, which will automatically be converted to a dash data-something="value" – Chris Fulstow Nov 17 '11 at 0:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions