Generics are a new functionality provided by version 2.0 of the C# language and the common language runtime (CLR). Generics introduce the concept of type parameters into the .NET Framework, making it possible to receive classes and methods whose specification of one or several types differs until the class or method is declared and instanciated by client code.
The “SDK Version” tickbox (contained in the “General” parameter set, as shown in the screenshot below) tells Modelio C# Designer whether or not this new functionality (and the other .NET Framework evolutions) is available during code generation.
The “SDK Version ” field.
Note: Do not modify the .NET Framework directory containing the version number during .NET installation.
An additional namespace, “System.Collections.Generic”, has been added to the Framework 2.0. This namespace contains the interfaces and classes that define the generic collections used to create strongly typed collections.
Modelio C# Designer provides and uses these new collections instead of the earlier ones, as they provide better security and performance than non-generic strongly typed collections (see below).
The dropdown list in the “Collection to sue” field in the C#“ property view.
Note: The “List” generic collection is used by default.
UML model | Generated C# |
---|---|
![]() |
![]() |
![]() |
![]() |
If Modelio C# Designer uses the .NET Framework 2.0, then associations and attributes whose multiplicity is greater than one will be generated using generic collections, as far as possible (see the table above).
To make your class, method or delegate generic, add a template parameter in the explorer.
The following table shows an example of a generic class and the generated C# code.
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of a generic method and the generated C# code.
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of a generic delegate and the generated C# code.
UML model | Generated C# |
---|---|
![]() |
![]() |
When you are defining a generic class, you can apply certain restrictions to the kinds of types that the client code can use as type arguments when it instantiates your class. These restrictions are called constraints. The following table presents the six types of constraint that exist:
List of constraint:
Note: Only the “BaseClass” constraint (base class name) must be indicated in the template parameter UML properties view (see the “Generic classes, methods and delegates” paragraph above). All the other are added using a tagged value (see “Tagged values types”).
The table below shows the intercompatibility of constraints:
struct | class | new() | <baseclass> | <interface> | U | |
---|---|---|---|---|---|---|
struct | No[1] | No[2] | No[2] | No[2] | Yes | Yes |
class | No[1] | Yes | No[3] | Yes | Yes | |
new() | No[1] | Yes | Yes | Yes | ||
<baseclass> | No[1] | Yes | Yes | |||
<interface> | Yes | Yes | ||||
U | Yes |
The following table shows an example of a generic class with a “BaseClass” type constraint.
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of a generic method with an “Interface” type constraint.
UML model | Generated C# |
---|---|
![]() |
![]() |
To specify a type argument, a concrete (or generic) type must be associated to: the attribute, parameter, role or inheritance link. In the explorer, select the element in question and then in the MDA annotations view, associate the {Bind} tagged value, with a types list as parameter (see the screenshot below).
The {Bind} tagged value in the MDA annotation view.
The following table shows an example of the association of a generic class with a parameter left unspecified (“T”).
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of an inheritance link: closed constructed type.
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of an inheritance link: open constructed type.
UML model | Generated C# |
---|---|
![]() |
![]() |
The following table shows an example of a generic parameter of a method left unspecified (“T”).
UML model | Generated C# |
---|---|
![]() |
![]() |