# generics – T是一个数字？

` `dynamic x = ..., y = ... dynamic result = x + y; // does what you expect` `

` `T x = ..., T y = ... int c = Comparer<T>.Default.Compare(x,y); if(c < 0) { // x < y } else if (c > 0) { // x > y }` `

` `public class MaxFinder<T> where T : IComparable<T> { public T FindMax(IEnumerable<T> items) { T result = default(T); bool first = true; foreach (T item in items) { if (first) { result = item; first = false; } else { if (item.CompareTo(result) > 0) { result = item; } } } return result; } }` `

` `public class Adder<T> { public delegate T AddDelegate(T item1, T item2); public T AddAll(IEnumerable<T> items, AddDelegate add) { T result = default(T); foreach (T item in items) { result = add(result, item); } return result; } }` `

` `Adder<int> adder = new Adder<int>(); int[] list = { 1, 2, 3 }; int sum = adder.AddAll(list, delegate(int x, int y) { return x + y; });` `

` `public class MyClass<T> where T : struct (...)` `

BCL中的静态Math类包含静态方法，您可以调用它们而无需创build类的实例。 你可以在class上做同样的事情。 例如，Math.Max有11个重载：

` `public static byte Max(byte val1, byte val2); public static decimal Max(decimal val1, decimal val2); public static double Max(double val1, double val2); public static short Max(short val1, short val2); public static int Max(int val1, int val2); public static long Max(long val1, long val2); public static sbyte Max(sbyte val1, sbyte val2); public static float Max(float val1, float val2); public static ushort Max(ushort val1, ushort val2); public static uint Max(uint val1, uint val2); public static ulong Max(ulong val1, ulong val2);` `
` `using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericPratice1 { public delegate T Del<T>(T numone, T numtwo)where T:struct; class Class1 { public T Addition<T>(T numone, T numtwo) where T:struct { return ((dynamic)numone + (dynamic)numtwo); } public T Substraction<T>(T numone, T numtwo) where T : struct { return ((dynamic)numone - (dynamic)numtwo); } public T Division<T>(T numone, T numtwo) where T : struct { return ((dynamic)numone / (dynamic)numtwo); } public T Multiplication<T>(T numone, T numtwo) where T : struct { return ((dynamic)numone * (dynamic)numtwo); } public Del<T> GetMethodInt<T>(int ch) where T:struct { Console.WriteLine("Enter the NumberOne::"); T numone =(T) Convert.ChangeType((object)(Console.ReadLine()), typeof(T)); Console.WriteLine("Enter the NumberTwo::"); T numtwo = (T)Convert.ChangeType((object)(Console.ReadLine()), typeof(T)); T result = default(T); Class1 c = this; Del<T> deleg = null; switch (ch) { case 1: deleg = c.Addition<T>; result = deleg.Invoke(numone, numtwo); break; case 2: deleg = c.Substraction<T>; result = deleg.Invoke(numone, numtwo); break; case 3: deleg = c.Division<T>; result = deleg.Invoke(numone, numtwo); break; case 4: deleg = c.Multiplication<T>; result = deleg.Invoke(numone, numtwo); break; default: Console.WriteLine("Invalid entry"); break; } Console.WriteLine("Result is:: " + result); return deleg; } } class Calculator { public static void Main(string[] args) { Class1 cs = new Class1(); Console.WriteLine("Enter the DataType choice:"); Console.WriteLine("1 : Int\n2 : Float"); int sel = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the choice::"); Console.WriteLine("1 : Addition\n2 : Substraction\3 : Division\4 : Multiplication"); int ch = Convert.ToInt32(Console.ReadLine()); if (sel == 1) { cs.GetMethodInt<int>(ch); } else { cs.GetMethodInt<float>(ch); } } } }` `

class yourclass <T>其中T：IComparable，IFormattable，IConvertible，IComparabe <T>，IEquatable <T>，struct {…最后，您仍然必须在运行时检查您的types是否可以使用object.GetType（ ） 方法。