-
Book Overview & Buying
-
Table Of Contents
Learn C# Programming
By :
It is sometimes necessary to construct temporary objects that hold some values, usually a subset of some larger object. To avoid creating a specific type for this purpose only, the language provides so-called anonymous types. These are a sort of use-and-forget types typically used in query expressions with Language Integrated Query (LINQ). This topic will be discussed in Chapter 10, Lambdas, LINQ, and Functional Programming.
These types are called anonymous because you do not specify a name in the source code. The name is assigned by the compiler. They consist of read-only properties only; any other member type is not allowed. The type of the read-only properties cannot be explicitly specified and is inferred by the compiler.
An anonymous type is introduced with the new keyword followed by a list of properties in angle-brackets (an object initializer). The following code snippet shows an example:
var o = new { Name = "M270 Turbo", Capacity = 1600...