CSharp examples for Custom Type:Generics
Generic method Maximum returns the largest of three objects.
using System;//from w ww. ja v a 2 s.c om class MainClass { static void Main() { Console.WriteLine($"Maximum of 3, 4 and 5 is {Maximum(3, 4, 5)}"); Console.WriteLine($"Maximum of 6.6, 8.8 and 7.7 is {Maximum(6.6, 8.8, 7.7)}"); Console.WriteLine("Maximum of pear, apple and orange is " + $"{Maximum("pear", "apple", "orange")}"); } // generic function determines the largest of the IComparable<T> objects private static T Maximum<T>(T x, T y, T z) where T : IComparable<T> { var max = x; // assume x is initially the largest if (y.CompareTo(max) > 0) { max = y; // y is the largest so far } if (z.CompareTo(max) > 0) { max = z; // z is the largest } return max; // return largest object } }