0

I'm following a tutorial from C# 7.0 in a Nutshell and getting an error that I can't fix. The way that I understood the error is that the method needs to be an abstract method, which I also tried, but that didn't resolve the error. And I thought abstract classes allowed non-abstract methods in them.

Here is the code:

public abstract class EqualityComparer<T>: IEqualityComparer, IEqualityComparer<T>
{
    public abstract bool Equals(T x, T y);
    public abstract int GetHashCode(T obj);

    bool IEqualityComparer.Equals(object x, object y);
    int IEqualityComparer.GetHashCode(object obj);

    public static EqualityComparer<T> Default { get; }
}

Here is the error:

'EqualityComparer<T>.IEqualityComparer.Equals(object, object)' must declare a body because it is not marked abstract, extern, or partial

Please let me know what I'm doing wrong.

rds80
  • 631
  • 1
  • 10
  • 23

2 Answers2

1

An abstract class can have abstract methods/properties and non-abstract methods/properties. Sub-classes of abstract classes must implement all methods/properties marked abstract in the base class

The abstract methods/properties do not need to be implemented in the base-class. However, any methods/properties you have in the base class that are not marked abstract must be implemented (i.e. have code).

Remember, the sub-classes don't need to implement the methods/properties not marked abstract; someone needs to implement them.

The error you are getting is for the second Equals overload - this one:

bool IEqualityComparer.Equals(object x, object y);

You also need to provide a body for:

int IEqualityComparer.GetHashCode(object obj);

Note that it isn't abstract and doesn't have an implementation/body. It needs to look like:

int IEqualityComparer.GetHashCode(object obj)
{
    //your code goes here - returning an int
}
Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • The error I'm getting is in the abstract class. Not in the subclass. – rds80 Aug 10 '18 at 19:03
  • @rds80 you need to add abstract to method declaration if you want it to be abstract. the class being abstract doesn't make all methods automatically abstract. – Selman Genç Aug 10 '18 at 19:04
  • 1
    I'm not talking about any sub-classes. In the base/abstract class, you need to provide an implementation for any methods not declared `abstract`. Put dummy bodies on those methods (for example, just put `return 0;` for both), and your code should compile. – Flydog57 Aug 10 '18 at 19:06
0

Yous have to implement the methods (explicit interface implementation makes the methods efficiently private which prevent them from being abstract: you can't declare, say, abstract bool IEqualityComparer.Equals... ), e.g.:

bool IEqualityComparer.Equals(object x, object y) {
  if (x is T && y is T)
    return this.Equals((T)x, (T)y); // known types, run Equals
  else
    return object.Equals(x, y);     // unknown type(s), run default Equals 
}

int IEqualityComparer.GetHashCode(object obj) {
  if (obj is T)
    return this.GetHashCode((T)obj);
  else
    return null == obj ? 0 : obj.GetHashCode();
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215