none
Should an interface always be used with my C# classes? RRS feed

  • Question

  • I've been reading about interfaces(the contract not the GUI) and pondering their use.  I'm trying to understand when they should be used.  It makes sense to use them if you are creating an api or service that can be consumed by other people or applications.  If the project is just a small website or some other small project then they don't seem as useful or necessary.  I would venture a guess that some OOP purists would say that they should always be used.  

    Are there any guidelines as to when an interface should be used and when it isn't that important?  Should they always be used regardless of the target audience of an application and it's size?

    Sunday, April 25, 2010 1:31 PM

Answers

  • Some 'purists' do say that interfaces should always be used - but take a more pragmatic approach - ignore the purists and use whatever you find useful.  Be wary of anyone who takes things to extremes.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, April 25, 2010 2:23 PM
  • why paint yyourself into a corner?
    gimme some slamming techno!!!!
    Sunday, April 25, 2010 4:03 PM
    Moderator
  • Interfaces can be useful at times, it depends on what you are doing. If you are refactoring to a design pattern, then it might make sense to use interafces of some sort. The more you abstract away, the more you will come to rely on it as a methodology.

    But OO principles do not require you use interfaces. Most of what I do, I usually use abstract classes if I need that level of abstraction.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Sunday, April 25, 2010 4:12 PM
  • For a level headed discussion on this see:

    Framework Design Guidelines: Conventions, Idoims, and Patterns for Reusable .NET Libraries, P. 77

    "Choosing between Class and Interface"

    1. Do favor defining classes over interfaces.
    2. Do use abstract classes instead of interfaces to decouple the contract from implementation
    3. Do define an interface if you need to provide a polymorphic hiercharcy of value types.
    4. Consider defining interfaces to achieve a similar effect to that of multiple inheritence.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Sunday, April 25, 2010 6:47 PM

All replies

  • Some 'purists' do say that interfaces should always be used - but take a more pragmatic approach - ignore the purists and use whatever you find useful.  Be wary of anyone who takes things to extremes.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, April 25, 2010 2:23 PM
  • why paint yyourself into a corner?
    gimme some slamming techno!!!!
    Sunday, April 25, 2010 4:03 PM
    Moderator
  • Interfaces can be useful at times, it depends on what you are doing. If you are refactoring to a design pattern, then it might make sense to use interafces of some sort. The more you abstract away, the more you will come to rely on it as a methodology.

    But OO principles do not require you use interfaces. Most of what I do, I usually use abstract classes if I need that level of abstraction.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Sunday, April 25, 2010 4:12 PM
  • Yes - definitely interfaces are very important, but the OP is referring I think to an extremist view in some very hardcore camps that I'm aware of which suggest that you should always only be coding to an interface. 
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, April 25, 2010 4:22 PM
  • That was the old COM days when you had no other choice except program to an interface.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Sunday, April 25, 2010 6:30 PM
  • I've also seen it from the Java 'group of grumpy OO guys' consortium.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, April 25, 2010 6:34 PM
  • For a level headed discussion on this see:

    Framework Design Guidelines: Conventions, Idoims, and Patterns for Reusable .NET Libraries, P. 77

    "Choosing between Class and Interface"

    1. Do favor defining classes over interfaces.
    2. Do use abstract classes instead of interfaces to decouple the contract from implementation
    3. Do define an interface if you need to provide a polymorphic hiercharcy of value types.
    4. Consider defining interfaces to achieve a similar effect to that of multiple inheritence.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Sunday, April 25, 2010 6:47 PM
  • Thanks for all of the replies.  I'll be sure to check out the Framework Design Guidelines that was suggested.
    Sunday, April 25, 2010 8:25 PM