Anonymous Types

From RemObjects Software

Jump to: navigation, search

This is a Oxygene Language Feature topic
Feel free to add your notes to this topic below.



Anonymous types are inline defined types to group related values together in a single block. An anonymous type can only be used inside the body of a method. Defining an anonymous type is as simple as using new class or new record.

var lUser := new class(Name := 'Peter'; Age := 49);
Console.WriteLine(lUser.Name+' is ' + lUser.Age + ' years old');

Here a new anonymous class is defined with two members. Name and Age. Name is a string and Age is an integer. Optionally a type can be used, like:

var lUser := new class(Name: String := 'Peter'; Age: Double := 49);
Console.WriteLine(lUser.Name+' is ' + lUser.Age + ' years old');

The name of a member in an anonymous type can also be inferred from the value, usually from the name of a variable or property, like:

var
  lUser := new class(Data.Name, Data.Value);

This will create a new anonymous class with two members, Name and Value. Oxygene will emit an error when the name of the last identifier is unclear, or cannot be inferred.

Internally an anonymous class is a private nested class (or record) in the current class. This class has an internal name that's not a valid identifier and can't be used directly. Anonymous classes are often used in combination with LINQ queries to only return the relevant data in the simplest syntax as possible.


See Also


Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools