Are anonymous types useful for use outside of LINQ?

When analyzing some ASP.NET MVC projects, I had to see anonymous types scattered around the world.

HTML Helpers:

<%=Html.TextBox("view.Address", "address", new { Class = "text_field" })%>

Many types of return actions for them have:

JsonNetResult jsonNetResult = new JsonNetResult
{
  Formatting = Formatting.Indented,
  Data = new {Something= ""}
}

I know this came from LINQ:

from p in context.Data
select new { p.Name, p.Age };

Are they really the right ways to accomplish the tasks that are now beyond LINQ? Do they enhance code reuse and readability?

+3
source share
5 answers

IMHO, . . , .

var list = New List<No way to specify an Ananymous type>();

.

public List<T> CreateList<T>(T notUsed) {
  return new List<T>();
}
var list = CreateList(new { .Class = "foo" });

, , ( ) ..

, . -. . , , , . .

LINQ .

+5

, .. - , . , . , , .. , , json . , , , .

+3

. , ? IDictionary < String, Object > ? , .

+1

ditto,

0

I use them regularly to populate GridViews and DataGridViews as it saves a lot of time by setting up the GridView to show only the columns of interest to me.

Other than the DataSources settings, I think it would be bad for readability - a good abstraction is probably better.

0
source

Source: https://habr.com/ru/post/1705466/


All Articles