Articles → CSHARP → Basic Generic Class Example
Basic Generic Class Example
Purpose
Creating A Generic Class
public class col < T > {
T t;
public T val {
get {
return t;
}
set {
t = value;
}
}
}
Calling The Generic Class
col<string> x = new col<string>();
x.val = "karan";
MessageBox.Show(x.val);
col<int> y = new col<int>();
// y.val = "karan"; // Error
y.val = 45; // Type safety
MessageBox.Show(y.val.ToString());