Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Anonymous Type - When To Use
Message
De
30/04/2010 14:09:08
John Baird
Coatesville, Pennsylvanie, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01462547
Message ID:
01462563
Vues:
51
>>>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.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform