interfaces c# what the point?

This is a discussion on interfaces c# what the point? within the C# Programming forums, part of the General Programming Boards category; Just been reading a chapter on Interfaces; what's the point of adding an interface to your class? Considering all i ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    interfaces c# what the point?

    Just been reading a chapter on Interfaces; what's the point of adding an interface to your class? Considering all i get for using them is a bunch of methods which i have to implement myself.

    So what's the purpose? what can they be used for? as per usual the book only gives pointless code examples and no real explanation of why you would use them.

    thanks.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    An interface is a contract. It says that any user of your code and expect the following methods. That interface might be used to read data and you might have written a class to get that data from a text file. Later you might recode that class into to pull data from a database or a remote procedure call. Later again you might decide to completely rewrite the code to make it faster and more robust.

    As long as you use the same interface, the users of your code don't need to know about what you are doing because no matter what's going on behind the scenes, you expose the same methods via that initial interface.

    It gives the users of your code an assurance that they wont have to completely recode their programs due to changes you make.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    thanks for reply, so it's only to ensure the way the methods used do not change? Is this used a lot in the industry, i mean using the language Interface ability?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    More importantly it can give multiple different classes the same type of operations. A (naive) way of supporting multiple types of printers could be done like this:
    Code:
    public class FilePrinter
    {
      public void PrintToFile();
    }
    
    public class NetworkPrinter
    {
      public PrintOverNetwork();
    }
    
    public void Print(object printer)
    {
      if(printer is FilePrinter)
      {
        var file_printer = printer as FilePrinter;
        file_printer.PrintToFile();
      }
      else if(printer is NetworkPrinter)
      {
        var network_printer = printer as NetworkPrinter;
        network_printer.PrintOverNetwork();
      }
    }
    A better way would be to give them a common printing interface:
    Code:
    public interface IPrinter
    {
      void Print();
    }
    
    public class FilePrinter : IPrinter
    {
      public void PrintToFile();
      public void Print() { PrintToFile(); }
    }
    
    public class NetworkPrinter : IPrinter
    {
      public PrintOverNetwork();
      public void Print() { PrintOverNetwork(); }
    }
    
    public void Print(IPrinter printer)
    {
      printer.Print();
    }
    A cleaner solution, less cluttering if-elses and much easier to extend with more Printers later (maybe an USBPrinter?).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Only time I ever used interfaces was when I was playing around with .net plugins. Having an interface of methods and variables is really cool if you want to add expandability to your application through .net dll plugins and so on.

  6. #6
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Why no abstract class?
    Just GET it OFF out my mind!!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by audinue
    Why no abstract class?
    What exactly do you mean by that question? For example, are you asking why an interface should be used instead of an abstract class, or do you think that C# does not have abstract classes as a language feature?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by audinue View Post
    Why no abstract class?
    Well
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Ok, why use an interface instead of an abstract class?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 12:00 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  3. instantiation point discussion
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2008, 03:48 AM
  4. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  5. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21