score:2

Accepted answer

The reason why you are getting this error is that you cannot use inequality operators ("<", ">") with IComparable, unless you override them.

You can use CompareTo() instead.

public class Mystack<T> : IComparable<Mystack<T>> where T : IComparable
{
public T[] stack = new T[2];

public int CompareTo(Mystack<T> other)
{
    // If the current stack < other stack return -1
    // If the current stack > other stack return +1
    // If current stack entries == other stack entries return 0
    for (var current = 0; current < 2; current++)
    {
        if (stack[current].CompareTo(other.stack[current]) < 0)
        {
            return -1;
        }
        else if (stack[current].CompareTo(other.stack[current]) > 0)
        {
            return 1;
        }
    }
    return 0;
}

Related Articles