In this lesson, we'll cover a concept that's extremely important in C# and other strongly-typed languages: interfaces. We can think of an interface as a blueprint of things (such as declarations, properties and methods) that must be included within any class that utilizes the interface. The interface doesn't actually care how these things are implemented - it only cares that they're included in the class.
This is a tricky concept so we'll start with an example. An IMotor
interface might include an OnSwitch()
method. We could have a set of classes that extend the IMotor
interface such as Radio
, Television
, and CoffeeMachine
. Each of these classes need to have an OnSwitch()
method. However, something different will happen in the OnSwitch()
method of each of these classes: an instance of Radio
will make music, an instance of Television
will turn on the screen, and an instance of CoffeeMachine
will start heating water. The IMotor
interface doesn't care about any of that stuff as long as the OnSwitch
method is included in the class.
Now let's look at a more concrete example. We've already been working with MSTest's IDisposable
interface. We can add the IDisposable
interface to a test class like this:
public class ItemTests : IDisposable
This is the syntax for extending a class:
public class ClassToBeExtended : InterfaceToInclude
After the class, we use a :
followed by the name of the interface that will extend the class.
Once IDisposable
is added, we have access to a new method called Dispose()
. This is because the IDisposable
interface extends the ItemTests
class. In other words, it gives our ItemTests
class more functionality.
However, the Dispose()
method doesn't actually do much when we add it to our code. The method runs after each test, but we have to tell it what code to execute. For instance, we could add a ClearAll()
method to reset the value of our static variables. However, we could add any code we wanted here. This is similar to the OnSwitch()
method our IMotor
interface provides.
What happens if we extend our ItemTests
class with IDisposable
but don't actually add a Dispose()
method? We'll get an error when we run our tests:
'ItemTests' does not implement interface member 'IDisposable.Dispose()'
Once we extend our class with the IDisposable
interface, we must add a Dispose()
method.
We can implement our own interfaces like this:
interface IMotor
{
string OnSwitch();
}
By convention, interface names begin with a capital I
. All classes that use the interface will have access to the methods included inside the interface.
We use a :
to extend a class with the IMotor
interface:
public class CoffeeMachine : IMotor
{
public string OnSwitch()
{
return "Bubble bubble hiss.";
}
}
A class can take advantage of multiple interfaces. Here's the syntax:
public class Radio : IMotor, IPlayMusic
Interfaces are a powerful tool in C# and other strongly-typed languages. For that reason, it's very important to understand what they are and how they work.
You are not required to use an interface for this section's independent project. However, you are encouraged to experiment with them and add them to projects if possible.
Extends: Adding additional functionality to a class with an interface.
Interface: A blueprint of things (such as declarations, properties and methods) that must be included within any class that utilizes the interface.
This is the syntax for extending a class with multiple interfaces:
public class ClassToBeExtended : InterfaceToInclude, InterfaceToIncludeTwo
This is a sample interface. Interface names begin with a capital I
.
interface IMotor
{
string OnSwitch();
}
Lesson 3 of 12
Last updated more than 3 months ago.