Structured Values and Types
The LINQ project supports a data-centric programming style in which some types exist primarily to provide a static "shape" over a structured value rather than a full-blown object with both state and behavior. Taking this premise to its logical conclusion, it is often the case that all the developer cares about is the structure of the value, and the need for a named type for that shape is of little use. This leads to the introduction of anonymous types that allow new structures to be defined "inline" with their initialization.
In C#, the syntax for anonymous types is similar to the object initialization syntax except that the name of the type is omitted.
For example, consider the following two statements:
object v1 = new Person {
Name = "Brian Smith", Age = 31, CanCode = false
};
object v2 = new { // note the omission of type name
Name = "Brian Smith", Age = 31, CanCode = false
};
The variables v1 and v2 both point to an in-memory object whose CLR type has three public properties Name, Age, and CanCode. The variables differ in that v2 refers to an instance of an anonymous type. In CLR terms, anonymous types are no different than any other type. What makes anonymous types special is that they have no meaningful name in your programming language. The only way to create instances of an anonymous type is using the syntax shown above.
To allow variables to refer to instances of anonymous types yet still benefit from static typing, C# introduces implicitly typed local variables: The var keyword may be used in place of the type name for local variable declarations.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
IEnumerable<string> expr = from s in names foreach (string item in expr) Console.ReadKey();
|
Output:

Aspnet Discussion
- - Web application
- - Great Indian Developer Su
- - Aspx page inside Silverli
- - Open aspx page inside Sil
- - Guidance for a novice





