Anonymous type

Wikipedia, the free encyclopedia - Cite This Source

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.

Example (C#)

var person = new {FirstName = "John", LastName = "Smith"}

Example (Visual Basic .NET)

Dim person = New With {.FirstName = "John", .LastName = "Smith"}

Example (Chrome)

var person := new class(FirstName := 'John', LastName := 'Smith');

See also

References



Wikipedia, the free encyclopedia © 2001-2006 Wikipedia contributors (Disclaimer)
This article is licensed under the GNU Free Documentation License.
Last updated on Tuesday February 19, 2008 at 11:01:44 PST (GMT -0800)
View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation