Anonymous types are a feature of
C# 3.0,
Visual Basic .NET 9.0,
Oxygene,
Scala and
Go that allows
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
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
Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007.
LINQ extends the langua ...
feature that is integrated into C# and VB.net. Since anonymous types do not have a named type, they must be stored in
variables declared using the
var
keyword, telling the C# compiler to use
type inference
Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics ...
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
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
. 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.
Examples
C#
var person = new ;
Console.WriteLine(person.lastName);
Output:
Go
var person struct
person.firstName="John"
person.lastName="Smith"
OCaml
let person = object val firstName = "John" val lastName = "Smith" end;;
Oxygene
var person := new class(firstName := 'John', lastName := 'Smith');
PHP
$person = new class
;
Scala
val person = new
Visual Basic .NET
Dim person = New With
See also
*
Extension method
In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-orie ...
*
Anonymous function
In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to ...
*
Expression tree
A binary expression tree is a specific kind of a binary tree used to represent Expression (mathematics), expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean algebra, boolean. These tre ...
References
{{reflist
External links
C# 3.0 Language Enhancements PresentationAnonymous Types in Visual Basic 2008- Learn about the new features in Visual Basic 2008.
C Sharp programming language family
Data types