Interface

An interface is a contract with any class that implements this interace. The contract states that the class must implement the members defined in the interface.
Using an interface enables you to create a more loose coupled program which is easier to maintain more scalable and makes code re-use more accessible.

Solid

With using intefaces you are allso following the Solid ( last D ) principles. The last D which stands for the Dependency Inversion principle. In short the dependency inversion principle state that a class should never depend on concrete implemtations buyt always on abstractions.

Interface rules

- start with capital I
- the letter following the I must be a capital allso
- no access modifiers ( private, public, protected )
- no body's ( no implementation )

In C# a class cannot inherit from multiple classes. In the case that a abstract class is to to complex and needs to be divided in more logical seperate classes which implement a certain interface. That class can inherit from multiple interfaces.

Interface vs abstract class:

  • use an interface when you don't care about the objects actual implementation
  • use an interface when you need your class to have multiple parents (.net is single inheritance, but multiple implementation, which means you can only inherit from one base class, but can implement as many interfaces as you want)
  • use a class when you want to share common functionality in child classes
  • use a class when you want to enforce implementation of some method, or property

C# 8: defeault interface methods

In C# 8 you can now add methods to your interface with an actual implementation.
Adding a actual implementations in an interface is called a ´Trait' language feature, this is stating that a method can be made availabe for different classes so it's defined once. Which is essentialy the core of Object Oriented programming.
The feature is already iplemented in java and by implementing this in C# the languate is growing more to a Android compatible language.
More information on default inteface methods can be found on the microsoft documentation, but the implemtation can be something like this:

A customer class which inherits from ILog. And the ILog implements the logging's methods:

public interface ILog
{
    public void Error(Exception exception)
    {
        // TODO: implement error statement
    }

    public void Debug(string message)
    {
        // TODO: implement debug statement
    }

    public void Info ( string message)
    {
        // TODO: implement info statement
    }
}

And the customer class wich implements the logging interface and using the default methods in the logging interface:

public class Customer: ILog
{
    public void Add(string name)
    {
        try
        {
            throw( new Exception("Something bad happened :-)"));
        }
        catch (Exception exception)
        {
            ILog log = this;
            log.Error(exception);
        }
    }
}

Recent

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!