0

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
None
  • 5,582
  • 21
  • 85
  • 170

3 Answers3

4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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;
    }
}
dcastro
  • 66,540
  • 21
  • 145
  • 155
0

You probably meant to implement IComparer<T>:

public class GenericComparer<T> : IComparer<T>
{
    public int Compare(T x, T y)
    {
        throw NotImplementedException;
    }
}
phoog
  • 42,068
  • 6
  • 79
  • 117