Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not to allow in-place interface implementation in .NET?

Either I am missing something or .NET doesn't support what Java does. I'd like to be able to avoid creating a small class just for the sake of implementing a small interface. For example, LINQ's Except method expects IEqualityComparer. So I had to write a small class that implements the interface. However in Java I can simply do a new IEqualityComparer() { //interface declarations } and be done with it. So what's the problem?

This is somewhat related to this post:

Can a C# anonymous class implement an interface?.

ADDITION: At the moment, I added overrides for Equals and GetHashCode.

like image 206
Schultz9999 Avatar asked May 03 '11 23:05

Schultz9999


People also ask

Why do we need to separating interface from implementation?

Defines an interface in a separate package from its implementation. As you develop a system, you can improve the quality of its design by reducing the coupling between the system's parts. A good way to do this is to group the classes into packages and control the dependencies between them.

Why should we program to an interface and not to an implementation?

If, as a programmer, you code against the implementation then as soon as it changes your code stops working. So think of the benefits of the interface this way: it hides the things you do not need to know making the object simpler to use. it provides the contract of how the object will behave so you can depend on that.

What is not allowed inside interface?

Interfaces can declare only Constant. Instance variables are not allowed. This means all variables inside the Interface must be public, static, final.

What happens if we dont implement all methods of interface?

If you don't a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.


1 Answers

You're correct, C# unlike Java, does not support the notion of anonymous inner classes which can implement interfaces. I've run into the exact same problem with IEqualityComparer and eventually came to the following solution.

public static class Utils {
  private sealed class EqualityComparer<T> : IEqualityComparer<T> {
    private readonly Func<T, T, bool> m_equals;
    private readonly Func<T, int> m_getHashCode;
    internal EqualityComparer(
      Func<T, T, bool> equals,
      Func<T, int> getHashCode) {
      m_equals = equals;
      m_getHashCode = getHashCode;
    }
    public bool Equals(T left, T right) {
      return m_equals(left, right);
    }
    public int GetHashCode(T value) {
      return m_getHasheCode(value);
    }
  }

  public static IEqualityComparer<T> CreateEqualityComparer<T>(
    Func<T, T, bool> equals, 
    Func<T, int> getHashCode) {
    return new EqualityComparer<T>(equals, getHashCode);
  }
}

Now in places where I want an on the fly IEqualityComparer<T> i can just do the following

var comparer = Utils.CreateEqualityComparer<Student>(
  (left, right) => left.Name == right.Name,
  value => value.Name.GetHashCode());
like image 128
JaredPar Avatar answered Oct 11 '22 19:10

JaredPar