Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Anonymous Type - When To Use
Message
From
30/04/2010 14:09:08
John Baird
Coatesville, Pennsylvania, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01462547
Message ID:
01462563
Views:
50
>>>When would you use
>>>
>>>
>>>var NewCust = new { CustomerId = 1, Name = "Mike Hanley" };
>>>
>>>
>>>as opposed to
>>>
>>>Customer NewCust = new Customer() { CustomerId = 1, Name = "Mike Hanley" };
>>>
>>>
>>>or
>>>
>>>var NewCust = new Customer() { CustomerId = 2, Name = "Kevin Hanley" };
>>>
>>>
>>>The Anonymous type seems confusing.
>>Example from:http://msdn.microsoft.com/en-us/library/bb383973.aspx
var custQuery = from cust in customers
>>                where cust.City == "Phoenix"
>>                select new { cust.Name, cust.Phone };
>>foreach (var item in custQuery)
>>{
>>    Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone);
>>}
If the Customer class has loads of other properties (obviously not required in this instance) then creating the light-weight anonymous type makes sense.....
>
>
>But how does C# know that
>
>
>var NewCust = new { CustomerId = 1, Name = "Mike Hanley" };
>
>
>is a Customer class?


Its not a customer class, its an anonymous class with 2 properties.
Previous
Reply
Map
View

Click here to load this message in the networking platform