Anonymous type
From Wikipedia, the free encyclopedia
Anonymous types are a feature of the C# 3.0, Visual Basic .NET 9.0, and Chrome 2.0 programming language that allows data types with named fields to be created implicitly from the code that requires it. This is an important feature for the SQL-like LINQ feature that will be integrated into C#. Since anonymous types do not have a named typing, they must be stored in variables declared using the var keyword, telling the C# compiler to use type inference for the variable.
This feature should not be confused with dynamic typing. While anonymous types allow programmers to define fields seemingly "on the fly", they are still static entities. Type checking is done at compile time, and attempting to access a nonexistent field will cause a compiler error. This gives programmers much of the convenience of a dynamic language, with the type safety of a statically typed language.
Contents |
[edit] Example (C#)
var person = new {FirstName = "John", LastName = "Smith"}
[edit] Example (Visual Basic .NET)
Dim person = New With {.FirstName = "John", .LastName = "Smith"}
[edit] Example (Chrome)
var person := new class(FirstName := 'John', LastName := 'Smith');
[edit] See also
[edit] References
- C# 3.0 Language Enhancements Presentation
- Anonymous Types in Visual Basic 2008 - Learn about the new features in Visual Basic 2008.

