Anonymous type

An anonymous type is a nameless data type that is known only within the current scope and verweisbar.

The XML Structure Definition Language XML Schema stipulates that one can define an unnamed ( anonymous ) complex type content in a named element.

In the C # programming language and VisualBasic.NET make the compiler since. NET Framework 3 provides a way to create an anonymous object with named properties in the current scope.

Anonymous types in XML Schema

In the structure definition XML schema language can be defined as part of the definition of an XML element, an anonymous type. This is often used in definitions of lists, as in the following example.

                                                                                                            < / xsd: element>                < / xsd: element> Here we have got element name for each invoice line, but the list itself does not appear as an element, as in the following example, which is an XML document that conforms to the schema defined earlier.

            23 < / item number >        3 < / number >                  42 < / item number >        5 < / number >         Anonymous types in C # 3 and Visual Basic 9

Anonymous types provide the language compiler provides the ability to create a simple object with properties, without having to define it in a separate file as a class. For this purpose, the variable is declared with ' var'.

Example in C #:

Var person = new { first name = "John", Name = " Count ", size = 40 }; With anonymous types, you can also instantiate predefined classes. The properties of the anonymous object is allocated to the public properties of the newly instantiated class. Suppose there exists the Person class with public properties first name, name and shoe size:

Person my person = new Person () { first name = "John", Name = " Count ", shoe size = 40 }; It is also possible to initialize arrays with the anonymous type syntax:

Person [ ] my people = new Person [] {    new Person { first name = "John", Name = " Count ", size = 40},    new Person { first name = " Anne ", name = " Schneider ", size = 38 } }; During initialization of Dictionary the syntax with anonymous types also simplified:

Dictionary IdPersonen = new Dictionary () {    {" ID_0 ", new Person { first name = "John", Name = " Count "},    {" ID_1 ", new Person { first name = " Anne ", name = " Schneider "} }; In fact, the application of this method should not be exaggerated, is because each time you define a new type generated (Type Builder) and instantiated.

Anonymous ( generated ) types. NET Framework are directly inherited from System.Object, can not be inherited and the properties are exclusively generated with get_ accessor, which is you can not define the type before a loop and change the property values ​​within the loop.

If you want to define a function that takes an anonymous type as a parameter, you have to define the function parameter as System.Object and read its properties using reflection.

67716
de