Website development and design blog, tutorials and inspiration

C# Interfaces and Classes

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

Written By on in C# 0

C# Interfaces and Classes

406 words, estimated reading time 2 minutes.

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.
C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

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. }
  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. }
  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. }

Last updated on: Thursday 22nd June 2017

Did you Like this Post? Why not Like us on Facebook?



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

Leave a Reply

Your email address will not be published.