Anonymous type
Encyclopedia
Anonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, and Oxygene that allows data type
s to encapsulate a set of properties into a single object without having to first explicitly define a type. 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
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.
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
s to encapsulate a set of properties into a single object without having to first explicitly define a type. This is an important feature for the SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
-like LINQ
LINQ
Linq is a word-based card game from Endless Games, introduced at the American International Toy Fair in 2005.Game play requires at least four players, two of whom are dealt cards with the same word, while the others receive blanks. The goal is to gain points by correctly naming the players with...
feature that is integrated into C# and VB.net. Since anonymous types do not have a named typing, they must be stored in variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
declared using the
var
keyword, telling the C# compiler to use type inferenceType inference
Type inference refers to the automatic deduction of the type of an expression in a programming language. If some, but not all, type annotations are already present it is referred to as type reconstruction....
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.
Example (C#)
Example (Visual Basic .NET)
Example (OCaml)
External links
- C# 3.0 Language Enhancements Presentation
- Anonymous Types in Visual Basic 2008 - Learn about the new features in Visual Basic 2008.