//-- ex-gen-type-parameter-in-constraint using System; using System.Collections.Generic; // A constraint may involve type parameters // A type may have multiple constraints struct ComparablePair : IComparable> where T : IComparable where U : IComparable { public readonly T Fst; public readonly U Snd; public ComparablePair(T fst, U snd) { Fst = fst; Snd = snd; } // Lexicographic ordering public int CompareTo(ComparablePair that) { int firstCmp = this.Fst.CompareTo(that.Fst); return firstCmp != 0 ? firstCmp : this.Snd.CompareTo(that.Snd); } public bool Equals(ComparablePair that) { return this.Fst.Equals(that.Fst) && this.Snd.Equals(that.Snd); } public override String ToString() { return "(" + Fst + ", " + Snd + ")"; } } // Sorting soccer world champions by country and year class MyTest { static void Test () { new ComparablePair("Brazil", 2002); } public static void Main(string[] args) { List> lst = new List>(); lst.Add(new ComparablePair("Brazil", 2002)); lst.Add(new ComparablePair("Italy", 1982)); lst.Add(new ComparablePair("Argentina", 1978 )); lst.Add(new ComparablePair("Argentina", 1986 )); lst.Add(new ComparablePair("Germany", 1990)); lst.Add(new ComparablePair("Brazil", 1994)); lst.Add(new ComparablePair("France", 1998)); // lst.Sort(); foreach (ComparablePair pair in lst) Console.WriteLine(pair); } }