None
None

Reputation: 5670

The type or namespace name 'T' could not be found while Implementing IComparer Interface

I am trying to implement IComparer Interface in my code

public class GenericComparer : IComparer
{
    public int Compare(T x, T y)
    {

        throw NotImplementedException;
    }
}

But this throws an error

Error 10 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

I have no idea, what's going wron. Can any one point out what I am doing wrong?

Upvotes: 0

Views: 3109

Answers (3)

dcastro
dcastro

Reputation: 68720

Given the name of your class, I think you meant to implement the generic IComparer<T> instead of the non-generic IComparer.

If so, you need to make your class generic, and declare the generic type parameter T

public class GenericComparer<T> : IComparer<T>
{
    public int Compare(T x, T y)
    {    
        throw NotImplementedException;
    }
}

Upvotes: 2

phoog
phoog

Reputation: 43056

You probably meant to implement IComparer<T>:

public class GenericComparer<T> : IComparer<T>
{
    public int Compare(T x, T y)
    {
        throw NotImplementedException;
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502406

Your GenericComparer isn't generic - and you're implementing the non-generic IComparer interface. So there isn't any type T... you haven't declared a type parameter T and there's no named type called T. You probably want:

public class GenericComparer<T> : IComparer<T>

Either that, or you need to change your Compare method to:

public int Compare(object x, object y)

... but then it would be a pretty oddly named class.

Upvotes: 4

Related Questions