Creating Generics

Generic classes can operate or be a holder for variables of defined type.

Generic class can be created with special option “Generic class” in class details window. Press the “New” button to add the generic parameter to your class:

There are three available generic parameters that can be used, but in most cases used only one parameter. Each parameter represents a Type that can be changed (defined) when the instance of class will be created.

Now our class became generic class (MyGebericClass<T>) and it allow using a generic parameter inside class as Type of variable:

or collection of variables:

Don’t forget to set “Default value” option for these variables if it required. Generic Types has only one default value (default(X)), which will set default value of this variable type. Check Default Values Table for more details.

Note: the “default” option will return null for reference types (class, enum, interface) and zero for numeric value types.

To test these variables we will add a function which will print all of their values:


Let’s test it!

Now create the instance of our new class, 

set the type of generic parameter,

add few values to a generic collection and print all values using our test function:

Note: if the generic parameter type of these nodes was not defined automatically you can change them from dropdown list by pressing generic parameter button on nodes header (from T to string type).

Render time: 0.02 seconds
136,992 unique visits