![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 5
| interfaces c# what the point? 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. |
| taver is offline | |
| | #2 |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,183
| 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.
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules |
| Fordy is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 5
| 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? |
| taver is offline | |
| | #4 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| 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();
}
}
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();
}
__________________ 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. |
| Magos is offline | |
| | #5 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| 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. |
| theoobe is offline | |
| | #6 |
| Ugly C Lover Join Date: Jun 2008 Location: Indonesia
Posts: 462
| Why no abstract class?
__________________ Just GET it OFF out my mind!! |
| audinue is offline | |
| | #7 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #8 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
|
__________________ 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. |
| Magos is offline | |
| | #9 |
| Registered User Join Date: Jul 2009
Posts: 5
| Ok, why use an interface instead of an abstract class? |
| taver is offline | |
| | #10 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
|
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting a floating point exception | SnertyStan | C Programming | 13 | 03-25-2008 11:00 AM |
| How to monitor process creation? | markiz | Windows Programming | 31 | 03-17-2008 02:39 PM |
| instantiation point discussion | George2 | C++ Programming | 1 | 03-08-2008 03:48 AM |
| Drawing Program | Max_Payne | C++ Programming | 21 | 12-21-2007 05:34 PM |
| overloading operator help | syrel | C++ Programming | 8 | 09-30-2004 10:49 AM |