DNR
DNR

Reputation: 3746

Benefits of implementing an interface

what are the benefits of implementing an interface in C# 3.5 ?

Upvotes: 17

Views: 35157

Answers (10)

Tone Škoda
Tone Škoda

Reputation: 1523

The way I understand it interfaces are most useful in these cases:

  1. Cleaner division of labor among programmers. Lead programmer writes interface and junior programmer writes its implementation. That makes perfect sense to me. Lead programmer could write pseudocode instead of interface though.

  2. Some specific situation, where you need 2 or more different implementations of the same class, for example interface animal and classes tiger and lion that use it. And even here it doesn't makes much sense, because lions and tigers share some things in common. Abstract class would be better, because if you use interface you have to write common functions in separate classes which leads to code duplication, which is bad.

  3. You write a library and want it to be modifiable by users. So you write interface and its class implementation. User of your lib still has the possibility to write his own implementation class, which may use different technology/algorithm which achieves the same result, but maybe in a faster way for example. This is also the reason why we meet so many interfaces in libs we use, but rarely feel the need to write our own interfaces. Because we don't write libraries.

Upvotes: 1

Jiten
Jiten

Reputation: 51

The main benefits of interfaces is mostly related to project design.

If you use an interface:

  1. The consumer of the interface should implement that interface.
  2. Designing bridge patters.
  3. Creating a contract so that user must adhere the rules of the interface.
  4. Can take only interface part (Object) from the main class.
  5. Even class is private, can obtain the interface object from that
  6. Multiple inheritance kind of style.
  7. Need not be should implement, simple go for if implements that means if you want you can implement other wise can drop it..
  8. Cleaner code.
  9. Implementation which changes depends on class can go ahead with interface.
  10. If each class have separate implementation of a method better to go for interfaces. For example IEnumerable in collections.

According to C# Architect, in a simple word it's a contract. Consumer must adhere to it.

Upvotes: 5

TJPowell
TJPowell

Reputation: 771

If you work in a huge, commercial software house - you MIGHT want to consider the judicial use of Interfaces. Otherwise, you should stay away from them. Same for multi-threading. If I see one more script-kiddie app that spawns 20 threads to write "Hello World" I'm gonna freak. Multi-threading should be completely reserved for apps that require it, usually in a multi-processing environment. 90% of the time it causes more harm than good. And don't bother with the thread highjack / off-topic comments. I don't care. I've been doing this longer than most of you have been alive. Rank has its privileges.

Upvotes: 2

TJPowell
TJPowell

Reputation: 771

Interfaces provide no actual advantage. Anything that can be done with an interface can, and should be done using other language constructions. Multiple inheritance is oft quoted as the only REAL benefit derived from using interfaces, but I can do multiple inheritance quite easily and clearly in C# - I do it every day. Changing the code without "breaking" the interface is the silliest of all excuses... That applies the same to concrete classes as it does to abstract classes or interfaces. As long as the functional signature does not change, you haven't broken the interface. Doesn't matter where it was declared. Simply putting a functional prototype in a separate file and naming it with an "I" in front buys nothing - except that you end up with twice as many source files to maintain. The supposition that the interface is defined early, and then maintains the contract is ridiculous. Interface methods and their parameters change ALL the time, because everything is never known up-front. That's why MicroSof stopped using them long ago. They had IUnKnown, IUnknown2, etc. It created a mess.

Upvotes: 5

Sivakumar
Sivakumar

Reputation: 11

An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members. All the member declarations inside interface are implicitly public.

Upvotes: 1

Philippe
Philippe

Reputation: 4051

An interface defines a contract (things that an object is able to do), while a concrete class (or struct) defines the concrete behavior.

For an example, IList is an interface, it defines the methods that a concrete object has to provide in order to be used like any other object implementing IList. Everywhere an IList can be used, your object that implements IList can be used as well. The way you concretely implement it and the way your object behaves when those IList methods are called is left to you.

Upvotes: 2

Jorge Córdoba
Jorge Córdoba

Reputation: 52143

The main benefit is about code readability, code maintainability and code "semantics".

  • Code readability: An interface constitutes a declaration about intentions. It defines a capability of your class, what your class is capable of doing. If you implement ISortable you're clearly stating that your class can be sorted, same for IRenderable or IConvertible.
  • Code semantics: By providing interfaces and implementing them you're actively separating concepts in a similar way HTML and CSS does. A class is a concrete implementation of an "object class" some way of representing the reality by modeling general properties of real life objects or concepts. An interface define a behavioral model, a definition of what an object can do. Separating those concepts keeps the semantics of your code more clear. That way some methods may need an instance of an animal class while other may accept whatever object you throw at them as long as it supports "walking".
  • Code maintainability: Interfaces helps to reduce coupling and therefore allow you to easily interchange implementations for the same concept without the underlying code being affected. You can change the implementation of a IMessage easily by defining a new class that implements the interface. Compare that to sistematically replacing all references from CMessage to CMyNewMessageClass.

Upvotes: 20

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422290

You'll be able to pass your object to a method (or satisfy a type constraint) that expects the interface as an argument. C# does not support "duck typing." Just by writing the methods defined by the interface, the object will not automatically be "compatible" with the interface type:

public void PrintCollection<T>(IEnumerable<T> collection) {
    foreach (var x in collection)
       Console.WriteLine(x);
}

If List<T> did not implement the IEnumerable<T> interface, you wouldn't be able to pass it as an argument to PrintCollection method (even if it had a GetEnumerator method).

Basically, an interface declares a contract. Implementing an interface enforces your class to be bound to the contract (by providing the appropriate members). Consequently, everything that relies on that contract (a method that relies on the functionality specified by the interface to be provided by your object) can work with your object too.

Upvotes: 27

Chris Gill
Chris Gill

Reputation: 2938

You aren't tied to class inheritance - you can apply an interface to any class. Any class can have multiple interfaces - C# doesn't support multiple class inheritance, i.e. you are providing a good abstraction layer through the interface

Upvotes: 1

Daniel Elliott
Daniel Elliott

Reputation: 22887

It will help when you try to:

  • Unit test with Stubs / Mocks
  • Implement Dependency injection
  • Solve world hunger (although this unproven!)

Kindness,

Dan

Upvotes: 13

Related Questions