Anonymous types are a feature ofC# 3.0,Visual Basic .NET 9.0,Oxygene,Scala andGo that allowsdata 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 theSQL-likeLINQ feature that is integrated into C# and VB.net. Since anonymous types do not have a named type, they must be stored invariables declared using thevar keyword, telling the C# compiler to usetype 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 withdynamic 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 astatically typed language.
varperson=new{firstName="John",lastName="Smith"};Console.WriteLine(person.lastName);
Output:Smith
varpersonstruct{firstNamestring;lastNamestring}person.firstName="John"person.lastName="Smith"
letperson=objectvalfirstName="John"vallastName="Smith"end;;
varperson:=newclass(firstName:='John',lastName:='Smith');
$person=newclass{public$firstName="John";public$lastName="Smith";};
valperson=new{valfirstName="John";vallastName="Smith"}
Dimperson=NewWith{.firstName="John",.lastName="Smith"}