0

An IComparable class provides a CompareTo method that compares two objects and determines their ordering, an IEquatable class provides an Equals method that determines whether an object is equal to another object, an IEnumerable class provides a GetEnumerator method that returns an IEnumerator object that has MoveNext and Reset methods for moving through a list of objects, an ICloneable class provides a Clone method that returns a copy of an object, et cetera.

Why do we need to implement these interfaces to provide this functionality? Could we not just define an Equals, GetEnumerator, MoveNext, Reset, and Clone method without implementing the interface?

I suspect this has something to do with using two objects at once (e.g., for comparing) and you need some sort of overarching class to do this, but I cannot wrap my head around it. Or maybe the interface does provide some extra functionality that otherwise would not be possible (e.g., IDisposable seems to be a necessary marker sometimes)? I hope someone can explain why everybody implements interfaces instead of just providing the functionality in the class itself.

user2609980
  • 10,264
  • 15
  • 74
  • 143
  • @CodeCaster: Basically, I agree, but I also think this is the much better question compared to the other one (as this question elaborates why one could deem interfaces unnecessary). (Of course, the accepted answer for the other question is already good.) – O. R. Mapper Dec 11 '13 at 09:34
  • @O.R.Mapper you're right in that that other question could be better worded in order to make it a reference "Why use interfaces" question. – CodeCaster Dec 11 '13 at 09:41

2 Answers2

5

Why do we need to implement these interfaces to provide this functionality?

So that other code can be written in terms of the interface, and work with objects of types which implement the interface without knowing about the specific types. The compiler ensures at compile-time that the relevant members are present. For example, consider this method:

public T Max<T>(T first, T second, IComparer<T> comparer)
{
    return comparer.Compare(first, second) > 0 ? first : second;
}

The compiler needs to make sure that the comparer.Compare method call makes sense - and it only does make sense because IComparer<T> specifies the method. You wouldn't want to have to write that method for every class that implements an appropriate Compare method... the interface allows us to express the commonality between the classes, and write the Max method once to handle all classes with that common behaviour.

This is because C# is a statically typed language - if it were dynamically typed, there wouldn't be a need for interfaces.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • `if it were dynamically typed, there wouldn't be a need for interfaces.` Why not? – user2609980 Dec 11 '13 at 09:30
  • @user2609980: Because then the binding to members would be done dynamically, at execution time.; – Jon Skeet Dec 11 '13 at 09:30
  • @user2609980: Maybe the more precise statement would be: "There wouldn't be a need for interfaces for compiling the code." There would still be a need for interfaces if you want compile-time checking that the methods are there. – O. R. Mapper Dec 11 '13 at 09:32
  • @O.R.Mapper: If there's compile-time checking that the methods are there, it wouldn't be a dynamically typed language, would it? (Typically dynamically typed languages don't even allow you to specify types for parameters etc.) – Jon Skeet Dec 11 '13 at 09:34
  • @JonSkeet: True - I should have read your complete sentence ;-) I was thinking of "dynamically typed calls/dynamically typed identifiers" (as an option, rather than as a rule for the whole language). Such as, allowing method calls that are resolved at runtime while ensuring that certain other method calls can already be verified at compile-time based on the interface implementation. – O. R. Mapper Dec 11 '13 at 09:38
1

You touch the basis of Object oriented design. You don't have to use interfaces everywhere, but this is a suggested abstraction design pattern, that let's you application to scale easily and be maintainable on long run.

For further understanding you ca check out: When to Use Interfaces

Tigran
  • 61,654
  • 8
  • 86
  • 123