Binary comparison operators on generic types

Posted by Brian Triplett on Stack Overflow See other posts from Stack Overflow or by Brian Triplett
Published on 2010-03-31T19:13:10Z Indexed on 2010/03/31 19:23 UTC
Read the original article Hit count: 374

Filed under:
|
|

I have a generic class that takes a type T. Within this class I have a method were I need to compare a type T to another type T such as:

public class MyClass<T>
{
    public T MaxValue
    {
        // Implimentation for MaxValue
    }

    public T MyMethod(T argument)
    {
        if(argument > this.MaxValue)
        {
             // Then do something
        }
    }
}

The comparison operation inside of MyMethod fails with Compiler Error CS0019. Is it possible to add a constraint to T to make this work? I tried adding a where T: IComparable<T> to the class definition to no avail.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET