Comparer in generics

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I developed a comparer class for a base class BEBase

public class BEBaseComparer:IComparer<BEBase>
....
....

but, i can't use this comparer with a derived class...

public class Pippo:BEBase
....


I'd like to sort a List<Pippo> using the BEBaseComparer

but lista.Sort(bc) where bc is a istance of BEBaseCompare do compiler error!!

How can'i do it ??

TIA, Marco Sellani
 
public class BEBaseComparer<T> :IComparer<T> where T:BEBase
{
public int Compare(T a,T b)
{
return a.BEBaseProperty ...............
}
}

It is rarely a good idea for a non-generic to inherit from a generic in C#.

It is more common in C++ because you can have a base reference the derived
class.
 
Thank you very much.

Marco Sellani.

Nick Hounsome said:
public class BEBaseComparer<T> :IComparer<T> where T:BEBase
{
public int Compare(T a,T b)
{
return a.BEBaseProperty ...............
}
}

It is rarely a good idea for a non-generic to inherit from a generic in C#.

It is more common in C++ because you can have a base reference the derived
class.
 
Back
Top