Website development and design blog, tutorials and inspiration

C# Interfaces and Classes

A look at c# Interfaces and what they are used for

By , 22nd July 2008 in C#

In this tutorial we will look at a construct similar to a class, but not a class. An interface can be thought of as a contract between a class and a service.
 

Any class that implements an interface must fully implement all aspects of the service specified by the interface. This ensures that when you use a class that inherits from an interface you know that it implements all the required functionality.

Interfaces cannot contain constants, data fields, constructors, destructors and static members. It can only contain the signatures of methods, properties, delegates or events. All the member declarations inside interface are implicitly public. If your abstract class only provides abstract methods then you can create an interface.

It is good practice to name interfaces with a capital I and also capitalise the first letter, for example, an interface called PetrolEngine would be named IPetrolEngine. This way you (and any other developers) know that it is an interface.

Although C# does not support multiple inheritances, your classes and structs can inherit from a base class as well as multiple interfaces. Let's look at a few classes and interface examples.

  1. interface ITestInterface
  2. {
  3. public void MethodToImpliment();
  4. }
  5.  
  6. class TestClass : ITestInterface
  7. {
  8. }

Classes implement interfaces in the same way that classes inherit from base classes. Because our TestClass implements ITestInterface, we must provide an implementation of MethodToImpliment or the compiler will throw an error.

  1. class TestClass : ITestInterface
  2. {
  3. public MethodToImpliment()
  4. {
  5. Console.WriteLine("Test Method Called!");
  6. }
  7. }

You may find that you need to inherit from a base class as well as implementing an interface. This can be done by first specifying the base class, then a comma-separated list of interfaces.

  1. class TestClass : BaseClass, ITestInterface
  2. {
  3. public MethodToImpliment()
  4. {
  5. Console.WriteLine("Test Method Called!");
  6. }
  7. }
  8.  
  9. class TestClass : BaseClass, ITestInterface1, ITestInterface2
  10. {
  11. public MethodToImpliment()
  12. {
  13. Console.WriteLine("Test Method Called!");
  14. }
  15. }

Refactor Functions

If you have created a class, you can use the refactor function of Visual Studio to automatically generate an interface declaration. Simply right click on the class name, select refactor then extract interface. This will create the code for an interface and amend your class to implement it.

You can also implement an interface using refactor. If you create a skeleton class which specifies an interface you can right click the interface and select implement interface. This will generate method stubs within your class that you can implement.

Extending Interfaces

An interface can be extended in the same way that a class can be extended using inheritance. This is useful when you want to add more functionality to an interface but keep the original less-specific interface intact.

  1. interface ITestInterface2 : ITestinterface
  2. {
  3. public AnotherMethodToImpliment();
  4. }
Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.