C# basic interface

Time:2021-12-31

Interfaces and classes actually have many similarities, but interfaces cannot be instantiated. Interfaces define more constraints and specifications,
For example, the two function modules will use the Create method. In general, you can use classes to create two methods to implement create
But to use an interface is to define the interface first

/// <summary>
///Interface declaration
/// </summary>
public interface ICreate
{
    void DoCreate();
}

Then implement the interface,

/// <summary>
///Interface implementation
/// </summary>
public class ModularA : ICreate
{
    public void DoCreate()
    {
        Console.Write("ModularA");
        Console.ReadKey();
    }
}
public class ModularB : ICreate
{
    public void DoCreate()
    {
        Console.Write("ModularB");
        Console.ReadKey();
    }
}

When we use it again

        var modularA = new ModularA();
        modularA.DoCreate();

        var modularB = new ModularB();
        modularB.DoCreate();

In fact, you will find that you only need to write the implementation process and do not need to define the Create method later.