Now that we know how to create an interface we will look at
how to implement one for a class we write. We could implement an existing interface
or one which we have created.
In order to say that we will be implementing an interface in
our class we only need to specify it on the same line as our class name
following a colon ":" symbol. It looks exactly the same as if we were
inheriting from a class.
Listing 2: Implementing an Interface
public class MyHelloWorld : IHelloWorlder
{
}
Since we have defined two methods in our IHelloWorlder
interface we will need to make sure that we define these two methods in our
class. This is part of the agreement we make with the interface when we define
a class. We agree to implement anything it says to implement. This lets our new
class act in a similar fashion to the other classes using the interface. If we
do not define these methods, Visual Studio will give us a message like this
one.
Figure 1

As you can see Visual Studio is requiring that we define
these two methods that we agreed to implement when we specified our interface.
It is at this point that we will go ahead and write implementations for these
two methods we agreed to implement. As you can see because interfaces require
certain components of the class to exist it makes it so that different classes
may be used interchangeably.
Listing 3: Defining the Methods
public class MyHelloWorld : IHelloWorlder
{
public void SayHello()
{
Console.WriteLine("Hello world!");
}
public void SayGoodBye()
{
Console.WriteLine("Good Bye World!");
}
}
Now that we have defined the two methods of the interface,
Visual Studio will allow us to build successfully. We now have a working
implementation of the IHelloWorlder class. We can now take a look at how we
will use this interface in our program.