Generic methods in C#
Forums:
void Swap<T>(ref T a, ref T b) {
var tmp = a;
a = b;
b = tmp;
}
var n1 = 5;
var n2 = 7;
Swap(ref n1, ref n2);
//n1 is 7 and n2 is 5
var s1 = "cat";
var s2 = "dog";
Swap(ref s1, ref s2);
//s1 is "dog" and s2 is "cat"
- Log in to post comments