Anonymous types is used to create new type without defining a type first
It is used in LINQ, Returns a few of read only properties while querying collection of object using LINQ
Use new keyword to define anonymous type
It is reference type, derived from system.object
If more than one anonymous types have same properties, and same order in same assembly then they consider us same type by the compiler.
Example:
Define anonymous type using new keword
var Offer= new {offerid=100, Offername="50% discount on all famous brands"}
returns set of few read only properties
var OfferQuery =
from Offer in Offers
select new
{
Offer.OfferName,
Offer.discount
};
foreach (var Offerinfo in OfferQuery)
{
Console.WriteLine("OfferName={0}, Discount={1}", Offerinfo.OfferName, Offerinfo.discount);
}
0 Comment(s)