简体   繁体   中英

How to implement == or >= operators for generic type

I have a generic type Foo which has a internal generic class Boo. Boo class a property Value of type K. In a method inside Foo i want to do a boo.Value >= value Note that second operand value is of type T. while compiling i am getting following error:

Operator '>=' cannot be applied to operands of type 'T' and 'T'

Can anyone please tell me whats the problem here?

Restrict the type argument to IComparable , then you can implement the operations with the CompareTo method. Of course, you won't be able to use your generic class with every type, but I think every built-in type which can be compared using such operators like >, <=, ... implements this interface.

Best Regards,
Oliver Hanappi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM