Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces in C#

Tags:

c#

interface

I am looking for a really simple explanation of interfaces in C#. I have been asking google but the answers I get are very technical and worded in a way a programmer might understand. It almost sounds like a method that can be called in order to perform a function, It allows the programmer to use less key strokes.

From what I am reading below interfaces are a way to create a container of method that do the same thing but with different technologies.

I would like to know what they are? What they do? What I might use them for?

like image 862
user770022 Avatar asked Nov 27 '25 15:11

user770022


2 Answers

...worded in a way a programmer might understand.

If you are not a programmer, you're going to have a little trouble understanding interfaces. Without a fairly good understanding of basic object-oriented principles, this might seem a little confusing. But onwards:

Interfaces are similar to classes, but instead of telling you what an object is it tells you what it does.

For example, a very common interface is IEnumerable. When a class implements that interface, it means that the object can create and return an Enumerator, which can be consumed by some other part of code (usually a foreach loop, or a LINQ query).

Another one you might see a lot is IDisposable. This means that you call call .Dispose() on the object and it will clean up resources. It doesn't say anything about what resources it'll clean up or what the object is -- it could be a StreamReader or a SqlConnection or something. All it says is that it has a method called Dispose.

Interfaces are very useful concepts. If you have a lot of classes that are very different but share a certain common behavior, you can have them all implement a single interface and then operate on them as one. For example, if you had an interface:

interface ICanPrintMyContents
{
    void PrintContents();
}

Then a bunch of classes that implemented them:

class ParkingGarage : ICanPrintMyContents { ... }
class UnimpressivePolitician : ICanPrintMyContents { ... }
class BankingSimulation : ICanPrintMyContents { ... }

You could put them all in a list together, even though there's no real relation between their classes.

List<ICanPrintMyContents> list = { theGarage, insertNameHere, sim1, anotherGarage };
foreach(ICanPrintMyContents item in list)
    list.PrintContents();

(Note: that ICan... naming convention is not widely used, and I don't advise it. Many people don't like it, but I'm using it here because it conveys the meaning of the interface very well. More common would be IPrintableContents or something)

like image 127
Ian Henry Avatar answered Nov 29 '25 03:11

Ian Henry


Imagine that you have a pizza store (I'm stealing this example from a famous Design Patterns book). You know that all pizzas need to be ordered, prepared, baked and boxed. Well, you can define this common behavior into an interface:

public interface IPizza
{
   void Order();
   void Prepare();
   void Bake();
   void Box();
}

And have your different kinds of pizzas implement that interface. When you implement an interface, you're forcing that class to have the same methods and parameters as the interface, for example:

public class PepperoniPizza : IPizza
{
   public void Order()
   {
       //Order Pepperoni pizza
   }

   public void Prepare()
   {
       //Prepare Pepperoni pizza
   }

   public void Bake()
   {
       //Bake Pepperoni pizza
   }

   public void Box()
   {
       //Box Pepperoni pizza
   }
}

You would pretty much have the same with Hawaiian, Cheese or any other Pizza.

In real life there are several uses for Interfaces. You can create contracts to extend an application, or ensure that it works on different situations.

Hope this helps

like image 31
Arturo Molina Avatar answered Nov 29 '25 04:11

Arturo Molina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!