Generics (parametrized types) in C-sharp are like templates in C++ only with somewhat limited capabilities (but, allegedly, increased safety). In particular, it is not possible to call arithmetic operators on generic types which makes C-sharp generics less useful for scientific computing.
Anyway, one can declare a generic class or a generic method using (just like in C++ and Java) angle brackets. For example, here is a (primitive) class implementing a generic list contaner,
public class genlist<T>{ /* "T" is the type parameter */ private T[] data; public int size => data.Length; public T this[int i] => data[i]; public genlist(){ data = new T[0]; } /* constructor */ public void push(T item){ /* add item to the list */ T[] newdata = new T[size+1]; /* inefective but saves memory */ for(int i=0;i<size;i++)newdata[i]=data[i]; newdata[size]=item; data=newdata; /* data is garbage collected */ } }The container can be used as
genlist<int> list = new genlist<int>(); list.push(1); list.push(2); list.push(3); for(int i=0;i<list.size;i++)WriteLine(list[i]);Actually, there is a built-in
System.Collection.Generic.List
container [→].
include '../foot.htm' ?>