Generic Class voorbeeldje
Generic class van het type t:
class mijnGeneric<t> { private List mijnLijstje<t>; public mijnGeneric() { mijnLijstje = new List<t>(); } public void add(t ditisdeparameter) { mijnLijstje.Add(ditisdeparameter); } public List<t> getLijstje() { return this.mijnLijstje; } }
deze initialiseer je dus door een object mee te geven, bijvoorbeeld als een list van integers:
mijnGeneric lijst = new mijnGeneric();
Je kunt dus ook een contraint plaatsen op de class zodat deze wel met het juiste object geinitialiseerd wordt
where T: struct The type argument must be a value type. Any value type except Nullable can be specified. See Using Nullable Types (C# Programming Guide) for more information. where T : class The type argument must be a reference type; this applies also to any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last. where T : The type argument must be or derive from the specified base class. where T : The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the argument supplied for U. This is called a naked type constraint.