泛型扩展方法:
- public static T Max<T>(this T num, T max) where T : IComparable
- {
- return num.CompareTo(max) < 0 ? max : num;
- }
调用:
- public class CompareTest : IComparable
- {
- public void Test()
- {
- this.Max(new CompareTest());
- }
-
- public int CompareTo(object obj)
- {
- return 0;
- }
- }