B
Bill McCormick
Hello,
I have a list of struct's that have generic fields:
public struct Item<T1, T2> : IComparable<Item<T1, T2>> {
private T1 _t1;
private T2 _t2;
public T1 t1 {
get { return _t1; }
}
public T2 t2 {
get { return _priority; }
}
internal Item(T1 at1, T2 at2) {
this._t1 = at1;
this._t2 = at2;
}
public int CompareTo(Item<T1, T2> other) {
return Comparer<T2>.Default.Compare(this.t2, other.t2);
}
}
My question is how to build an IComparer class. Specifically, in the
following, what must X be so that the compiler will be OK with Y & Z.
Have I run into some language syntax limitation? Maybe an interface is
required?
public class MyCompare<X> : IComparer<X>
where X : IComparable<Item<Y,Z>> {
public Compare(Item<Y,Z> item1, Item<Y,Z> item2) { ... }
public bool Equals(Item<Y, Z> item1, Item<Y, Z> item2) { ... }
public int GetHashCode(Item<Y, Z> item) {
}
public class MyList<T1, T2> : IEnumerable<Item<T1, T2>>, ICloneable {
protected List<Item<T1, T2>> myList = new Item<T1, T2>>();
protected IComparer<Item<T1, T2>> myComparer = new MyCompare<Item<T1,
T2>>();
...
}
Thanks,
Bill
(This is a repost (under my MSDN account) of an early post; hopefully
this is more clear.)
I have a list of struct's that have generic fields:
public struct Item<T1, T2> : IComparable<Item<T1, T2>> {
private T1 _t1;
private T2 _t2;
public T1 t1 {
get { return _t1; }
}
public T2 t2 {
get { return _priority; }
}
internal Item(T1 at1, T2 at2) {
this._t1 = at1;
this._t2 = at2;
}
public int CompareTo(Item<T1, T2> other) {
return Comparer<T2>.Default.Compare(this.t2, other.t2);
}
}
My question is how to build an IComparer class. Specifically, in the
following, what must X be so that the compiler will be OK with Y & Z.
Have I run into some language syntax limitation? Maybe an interface is
required?
public class MyCompare<X> : IComparer<X>
where X : IComparable<Item<Y,Z>> {
public Compare(Item<Y,Z> item1, Item<Y,Z> item2) { ... }
public bool Equals(Item<Y, Z> item1, Item<Y, Z> item2) { ... }
public int GetHashCode(Item<Y, Z> item) {
}
public class MyList<T1, T2> : IEnumerable<Item<T1, T2>>, ICloneable {
protected List<Item<T1, T2>> myList = new Item<T1, T2>>();
protected IComparer<Item<T1, T2>> myComparer = new MyCompare<Item<T1,
T2>>();
...
}
Thanks,
Bill
(This is a repost (under my MSDN account) of an early post; hopefully
this is more clear.)