Anonymous type
Encyclopedia
Anonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, and Oxygene that allows data type
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 inference
Type 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#)


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

Example (Visual Basic .NET)


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

Example (OCaml)


let person = object val firstName = "Ejaz" val lastName = "Akhtar" end;;

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK