1 //-- ex-gen-type-parameter-in-constraint
4 using System
.Collections
.Generic
;
6 // A constraint may involve type parameters
7 // A type may have multiple constraints
9 struct ComparablePair
<T
,U
> : IComparable
<ComparablePair
<T
,U
>>
10 where T
: IComparable
<T
>
11 where U
: IComparable
<U
> {
12 public readonly T Fst
;
13 public readonly U Snd
;
15 public ComparablePair(T fst
, U snd
) {
19 // Lexicographic ordering
20 public int CompareTo(ComparablePair
<T
,U
> that
) {
21 int firstCmp
= this.Fst
.CompareTo(that
.Fst
);
22 return firstCmp
!= 0 ? firstCmp
: this.Snd
.CompareTo(that
.Snd
);
25 public bool Equals(ComparablePair
<T
,U
> that
) {
26 return this.Fst
.Equals(that
.Fst
) && this.Snd
.Equals(that
.Snd
);
29 public override String
ToString() {
30 return "(" + Fst
+ ", " + Snd
+ ")";
34 // Sorting soccer world champions by country and year
39 new ComparablePair
<string,int>("Brazil", 2002);
42 public static void Main(string[] args
) {
43 List
<ComparablePair
<string,int>> lst
44 = new List
<ComparablePair
<string,int>>();
45 lst
.Add(new ComparablePair
<String
,int>("Brazil", 2002));
46 lst
.Add(new ComparablePair
<String
,int>("Italy", 1982));
47 lst
.Add(new ComparablePair
<String
,int>("Argentina", 1978 ));
48 lst
.Add(new ComparablePair
<String
,int>("Argentina", 1986 ));
49 lst
.Add(new ComparablePair
<String
,int>("Germany", 1990));
50 lst
.Add(new ComparablePair
<String
,int>("Brazil", 1994));
51 lst
.Add(new ComparablePair
<String
,int>("France", 1998));
53 foreach (ComparablePair
<String
,int> pair
in lst
)
54 Console
.WriteLine(pair
);