Anonymous typeAnonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, Oxygene, Scala and Go that allows data 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 the SQL-like LINQ 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 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. ExamplesC#var person = new { firstName = "John", lastName = "Smith" };
Console.WriteLine(person.lastName);
Output: Smith Govar person struct { firstName string; lastName string }
person.firstName = "John"
person.lastName = "Smith"
OCamllet person = object val firstName = "John" val lastName = "Smith" end;;
Oxygenevar person := new class(firstName := 'John', lastName := 'Smith');
PHP$person = new class
{
public $firstName = "John";
public $lastName = "Smith";
};
Scalaval person = new { val firstName = "John"; val lastName = "Smith" }
Visual Basic .NETDim person = New With {.firstName = "John", .lastName = "Smith"}
See alsoReferences
External links
|
Portal di Ensiklopedia Dunia