1.7. Anonymous Types

In Listing 1-1, note that no type was specified after the new keyword in the object initialization expression. The compiler created a locally scoped anonymous type for us.

Anonymous types let us work with query results on the fly without having to explicitly define classes to represent them. When the compiler encountered

select new { p.FirstName, p.LastName };

in Listing 1-1 it transparently created a new class with two properties, one for each parameter (see Listing 1-7).

Example 1-7. A Class for an Anonymous Type
internal class ???
{
    private string _firstName;
    private string _lastName;
public string FirstName { get { return _firstName; } set { firstName = value; } } public string LastName { get { return _lastName; } set ...

Get LINQ for Visual C# 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.