Anonymous data type

from Wikipedia, the free encyclopedia

An anonymous type is a nameless data type that is only known and referenced in the current area of ​​validity.

The XML structure definition language XML Schema provides that an unnamed (anonymous) complex type content can be defined in a named element.

In the programming languages C # and Visual Basic .NET , the compilers since .NET Framework 3 provide a possibility to create an anonymous object with named properties in the current scope.

Anonymous types in XML Schema

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

  <xsd:element name="rechnung">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="posten" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="artikelnummer" type="xsd:string" />
              <xsd:element name="anzahl" type="xsd:integer" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

Although there are element names for individual invoice items, the list itself does not appear as an element, as in the following example, which represents an XML document that corresponds to the previously defined schema.

  <rechnung>
    <posten>
      <artikelnummer>23</artikelnummer>
      <anzahl>3</anzahl>
    </posten>
    <posten>
      <artikelnummer>42</artikelnummer>
      <anzahl>5</anzahl>
    </posten>
  </rechnung>

Anonymous types in C # 3 and Visual Basic 9

With anonymous types, the language compilers provide the possibility of creating a simple object with properties without having to define it as a class in a separate file. To do this, the variable is declared with 'var'.

Example in C #:

var Person = new { Vorname = "Hans", Name = "Graf", Schuhgröße = 40 };

The anonymous types can also be used to instantiate predefined classes. The properties of the anonymous object are mapped to the public properties of the newly instantiated class. Assume the class Person exists with the public properties first name, surname and shoe size:

Person meinePerson = new Person() { Vorname = "Hans", Name = "Graf", Schuhgröße = 40 };

It is also possible to initialize arrays with the anonymous type syntax:

Person[] meinePersonen = new Person[] {
  new Person { Vorname = "Hans", Name = "Graf", Schuhgröße = 40 },
  new Person { Vorname = "Anne", Name = "Schneider", Schuhgröße = 38 }
};

When initializing Dictionary <T>, the syntax with anonymous types is also simplified:

Dictionary<string, Person> IdPersonen = new Dictionary<string, Person>() {
  { "id_0", new Person { Vorname = "Hans", Name = "Graf" },
  { "id_1", new Person { Vorname = "Anne", Name = "Schneider" }
};

In fact, the use of this method should not be exaggerated, since a new type is generated (TypeBuilder) and instantiated with each definition.

Anonymous (generated) types in the .NET Framework are directly inherited from System.Object, cannot be inherited and the properties are generated exclusively with the get_ accessor, i.e. H. you cannot define the type in front of a loop and change the property values ​​within the loop.

If you want to define a function that accepts an anonymous type as a parameter, you have to define the function parameter as System.Object and read out its properties by means of reflection .

Web links

Individual evidence

  1. XML Schema Part 0: Primer Second Edition: 2.4 Anonymous Type Definitions. Retrieved November 29, 2009 .