void Swap<T> (ref T x, ref T y) ...{ T temp = x; x = y; y = temp; }
泛型代理(Generic Delegate):既然能够定义泛型方法,自然也可以定义泛型代理
publicdelegatevoid delegateSample <T> (ref T x, ref T y) privatevoid Swap (ref T x, ref T y) ...{ T temp = x; x = y; y = temp; } // 调用 publicvoid Run() ...{ int i,j; i =3; j =5; delegateSample<int> sample =new delegateSample<int> (Swap); sample(i, j); }
设置可空值类型:一般来说,值类型的变量是非空的。但是,Nullable<T>可以解决这个问题。
Nullable<int> x; // 这样就设置了一个可空的整数变量x x =4; x +=3; if (x.HasValue) // 使用HasValue属性来检查x是否为空 ...{ Console.WriteLine ("x="+x.ToString()); } x =null; // 可设空值