Welcome to the interfaces chapter.
In this video we're going to look at what an interface is and how it looks like, and also why interfaces
matter.
Okay.
So let's start with what is an interface.
Well, an interface in C sharp is a reference type that defines a contract of methods and properties
that a class must implement.
It contains only the signatures of methods, properties, events, or indexes, not the implementation
itself.
So why would you use interfaces?
Well number one is abstraction.
So interfaces define what methods a class should implement without specifying how they should be implemented.
Then number two is polymorphism which allows different classes to be treated as instances of the interface
type.
So we're going to look into polymorphism a lot more in a later chapter.
Then there is decoupling which reduces dependencies between classes, making the code more modular and
easier to use.
And we're also going to go deeper into decoupling.
Then there is reusability.
So interfaces ensure that different classes can use common methods enhancing the code reusability.
Then there is testability.
So it facilitates unit testing by allowing mock implementations of interfaces.
And we have an entire chapter focusing on the test driven development side of things and unit testing
with Yannick in a later chapter as well.
So let's look at a practical example here.
So I'm inside of a new project here and I called it Interfaces app.
And the first thing that I'm going to show you is how an interface code or syntax looks like.
So you start with the public keyword, then the interface keyword, and then and then you give your
interface a name.
So you see I start with a capital I, then a capital letter for the word that I want to use or the name
that I want to use for that interface.
So I indicates that this is going to be an interface.
And you can see it is green.
So it is a light green.
This is like a turquoise green for classes and for interfaces.
You will always see, at least in Visual Studio, that they have this light green as their color.
So whenever you see that you know it's an interface, or whenever you see an I at the beginning of a
class name or name of something, then it should be an interface.
So then here we go ahead and we define what methods should be in a class that implements this interface.
Coming back to our example that we used earlier with animals.
So I have this interface I animal where I would say for example, there's a method Makesound doesn't
return anything.
So it's a void.
And then I have the name of the method, the brackets opening and closing as well as a semicolon.
So you see there are no curly brackets here.
So there's no implementation for this method directly in the interface.
I can do that in interfaces.
I can just say you know what, this method does not have an implementation.
But whoever implements this interface needs to implement the method in their own way.
And this is what I was talking about earlier with reusability and also having a common contract.
So then another one would be eat.
Okay.
So this is another example of what a method could look like.
You see again a return type.
The return type can be something else.
It doesn't have to be void.
It could be a string, it could be an int, it could be whatever.
And we can have parameters here as well.
So you could say like what the animal should eat.
So food for example string food would be whatever an animal should eat.
And we can say okay in the implementation it should do something.
And you can obviously say okay, eat is going to return a string.
For example, the dog has eaten or the cat has eaten or something like that, but I'm just going to
leave it void because it should just be a statement showing something on the screen.
All right.
So this was just an example of the syntax for an interface, including the naming convention and including
our Non-implemented method declarations.
By the way, regarding the naming convention, I animal is one example.
But then if it was I animal food for example, then I would make the first letter in the next word also
capitalized.
So we're still using camel case here okay.
So let's see how we can implement an interface in a class in the next video.