Generics

 

Introduction

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 Objecteering C# Developer 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.

 

 

Generic collection classes

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.

 

Class

Description

Dictionary

Represents a collection of keys and values.

LinkedList

Represents a double-link list.

List

Represents a list strongly typed by objects accessible through the index. Provides search, sort and handling methods for lists.

Queue

Represents a collection of objects, first in, first out.

SortedDictionary

Represents a collection of key/value pairs sorted by key.

SortedList

Represents a collection of key/value pairs softed by key according to the associated Icomparer implementation.

Stack

Represents a first in first out-type collection of  variable size of instances of the same arbitrary type.

 

 

Objecteering C# Developer v1.5 or later 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" field in the "C#" tab of the auxiliary window

 

Note: The "List" generic collection is used by default.

 

 

UML model

Generated C#

 

 

 

 

 

 

If Objecteering C# Developer v1.5 (and later) 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).

 

 

Generic classes, methods and delegates

To make your class, method or delegate generic, add a template parameter in the explorer.  The window below then appears.

 

The "Template parameter" window

 

Key:

1.      Name of the template parameter.

2.      "BaseClass" constraint on the template parameter.

3.      These fields are not used by Objecteering C# Developer.

 

 

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#

 

 

 

Template parameters and constraints

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:

 

Constraint

Description

where T : struct

The type argument must be a value type. Any value type, except Nullable, can be specified.

where T : classe

The type argument must be a reference type, including any class, interface, delegate or table type.

where T : new()

The type argument must have a constructor without public parameters.

where T : <base class name>

The type argument must be or derive from the specified base class.

where T : <interface name>

The type argument must be or implement the specified interface.  Several interface constraints can be specified.  The interface that imposes the constraints can also be generic.

where T : U

The type argument provided for T must be the argument provided for U, or must derive from it.  This is what is called a naked type constraint.

 

Note:      Only the "BaseClass" constraint (base class name) must be indicated in the template parameter window (see the "Generic classes, methods and delegates" paragraph above).  All the other are added using a tagged value (see "Tagged values for C# 2.0").

 

 

The table below shows the intercompatibility of constraints:

·         [1]: The constraint can only be present once

·         [2]: The type argument cannot simultaneously be a valued type and a reference.

·         [3]: The reference cannot be specialized (BaseClass) and general (class).

 


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#

 

 

 

Specifying type arguments

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 auxiliary window, associate the {CsBind} tagged value, with a types list as parameter (see the screenshot below).

 

The {CsBind} tagged value in the auxiliary window

 

Key:

1.      Example of the {CsBind} tagged value.

2.      Example of the non-specified type parameter T.

 

 

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#