Anonymous type
From Wikipedia, the free encyclopedia
Anonymous types are a feature of the C# 3.0, Visual Basic .NET 9.0, and Oxygene that allows data types to encapsulate a set of properties into a single object without having to first explicitly define a type.[1] This is an important feature for the SQL-like LINQ feature that is integrated into C# and VB.net. 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. The properties created are read-only in C#, however they are read-write in VB.net.
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 (Oxygene)
var person := new class(FirstName := 'John', LastName := 'Smith');
[edit] See also
[edit] References
- ^ "Anonymous Types (C# Programming Guide)". Microsoft. http://msdn.microsoft.com/en-us/library/bb397696.aspx. Retrieved on 2008-11-25.
[edit] External links
- C# 3.0 Language Enhancements Presentation
- Anonymous Types in Visual Basic 2008 - Learn about the new features in Visual Basic 2008.

