While building such
a class is not rocket science, it can be rather
labor intensive if you are
attempting to encapsulate more than a handful of members (although automatic properties do
help in this regard). As of C# 2008, we are now provided with a massive shortcut for this very situa-
tion termed
anonymous types, which in many ways is a natural extension of C#’s
anonymous
methods syntax (examined in Chapter 11).
When you define an anonymous type, you do so by making use of the new var keyword in con-
junction with the object initialization syntax you have just examined. To illustrate, create a new
Console Application named AnonymousTypes. Now, update Main() with the following anonymous
class, which models a simple car type:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Anonymous Types *****\n");
Do'stlaringiz bilan baham: