Besides LINQ queries, how to use anonymous types in C #?

I'm trying to speed up work on some of the new features in C #, and one of them, which I did not have the ability to use, is anonymous types.

I understand the use as it relates to LINQ queries and I looked at this SO post which asked a similar question. Most of the examples I've seen on the net are related to LINQ queries, which is great. I saw some somewhat far-fetched examples, but not really where I saw a lot of value.

Do you have a new use for anonymous types where you think it really provides you with some usefulness?

+5
source share
6 answers

< string, object > ; : http://weblogs.asp.net/rosherove/archive/2008/03/11/turn-anonymous-types-into-idictionary-of-values.aspx

Jacob Carpenter , : http://jacobcarpenter.wordpress.com/2007/11/19/named-parameters-part-2/

, foreach. (, , , LINQ to Objects.) :

Dictionary<int, string> employees = new Dictionary<int, string>
{
    { 1, "Bob" },
    { 2, "Alice" },
    { 3, "Fred" },
};

// standard iteration
foreach (var pair in employees)
    Console.WriteLine("ID: {0}, Name: {1}", pair.Key, pair.Value);

// alias Key/Value as ID/Name
foreach (var emp in employees.Select(p => new { ID = p.Key, Name = p.Value }))
    Console.WriteLine("ID: {0}, Name: {1}", emp.ID, emp.Name);

, foreach , ID Name .

+6

ASP.NET MVC .

+5

, -, LINQ, LINQ - - . , -, LINQ.

, , - "" , , , Equals/GetHashCode/ToString () . "" , .

, , - , . , , # 5.

+3

, Justice, ASP.Net MVC - , , . :


Html.ActionLink("A Link", "Resolve", new { onclick = "someJavascriptFn();" })

ASP.Net MVC , HTML. , , , , .

+3

- LINQ, , .

, linq , :

var x = new { a = 1, b = 2 };

.

+2

I used them to create template letters, as they are great if you use reflection and generics.

Information can be found here: http://www.aaron-powell.com/blog.aspx?id=1247

+1
source

All Articles