Implementing a generic interface with type constraints?

Public interface IComparison { Result Compare(RETURNED_PARAM returned); } public class GreaterThan : IComparison where RETURNED_PARAM : IComparable { private EXPECTED_PARAM expected_; public GreaterThan(EXPECTED_PARAM expected) { expected_ = expected; } public Result Compare(RETURNED_PARAM returned) { return ((returned == null && expected_ == null) || (returned! = null && returned. CompareTo( expected_ ) > 0))?

Result. Fail : Result. Pass; } }.

That's probably what I will have to do. The only thing I don't like is that I didn't have to know the RETURNED_PARAM type until Compare was called before. For this, I have to know both types at construction time.

Thanks! – PaulH Jun 21 at 16:38 If it's not a problem could you accept the answer. Thanks :) – boca Jun 21 at 16:40.

Your inheritance hierarchy is the problem. GreaterThan inherits from IComparison, which means you're telling the compiler that it implements Compare, but you want it to implement Compare instead. You could drop the generic constraint on your interface: public interface IComparison { Result Compare(RETURNED_PARAM returned) where RETURNED_PARAM : IComparable; } public class GreaterThan: IComparison { private EXPECTED_PARAM expected_; public GreaterThan(EXPECTED_PARAM expected) { expected_ = expected; } public Result Compare(RETURNED_PARAM returned) where RETURNED_PARAM : IComparable { return ((returned == null && expected_ == null) || (returned!

= null && returned. CompareTo( expected_ ) > 0))? Result.

Fail : Result. Pass; } }.

If I remove the type constraint from the IComparison interface, then I get the error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly. Also, I prefer the generic IComparable interfaces to avoid the boxing/unboxing. – PaulH Jun 21 at 18:51 @PaulH I just (re-)tested my example code, it compiles.

Does yours look different from mine? – Gabe Moothart Jun 21 at 19:28 Yes, it compiles as posted, but you mentioned dropping the generic constraint from the IComparison. Compare interface.

If I eliminate that where RETURNED_PARAM : IComparable, then it does not compile. – PaulH Jun 21 at 21:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions