Easy Tutorial
❮ Csharp Preprocessor Directives Csharp Namespace ❯

This is a Chinese to English translation.

An interface defines the syntactic contract that all classes inheriting the interface should follow. The interface defines the "what" part of the syntactic contract, while the derived class defines the "how" part.

An interface defines properties, methods, and events, which are members of the interface. The interface only contains declarations of its members. The definition of the members is the responsibility of the derived class. The interface provides a standard structure that derived classes should follow.

Interfaces ensure that classes or structures that implement the interface maintain a consistent form.

Abstract classes are somewhat similar to interfaces, but they are mostly used when only a few methods are declared by the base class and implemented by the derived class.

An interface itself does not implement any functionality; it merely contracts with the object that declares it to implement certain behaviors.

Abstract classes cannot be directly instantiated but allow the derivation of concrete classes with actual functionality.


Defining an Interface: MyInterface.cs

Interfaces are declared using the interface keyword, similar to class declarations. Interface declarations are by default public. Below is an example of an interface declaration:

interface IMyInterface
{
    void MethodToImplement();
}

The above code defines the interface IMyInterface. Typically, interface names start with the letter I. This interface has only one method, MethodToImplement(), which has no parameters or return value. Of course, parameters and return values can be set according to requirements.

It is important to note that this method does not have a concrete implementation.

Next, let's implement the above interface: InterfaceImplementer.cs

Example

using System;

interface IMyInterface
{
    // Interface member
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
    }

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

The InterfaceImplementer class implements the IMyInterface interface. The syntax for implementing an interface is similar to that for inheriting a class:

class InterfaceImplementer : IMyInterface

After inheriting the interface, we need to implement the method MethodToImplement(), which must have the same name as the method defined in the interface.


Interface Inheritance: InterfaceInheritance.cs

The following example defines two interfaces, IMyInterface and IParentInterface.

If an interface inherits from other interfaces, then the implementing class or structure must implement all members of the interfaces.

The following example shows that IMyInterface inherits from IParentInterface, so the implementing class must implement both MethodToImplement() and ParentInterfaceMethod():

Example

using System;

interface IParentInterface
{
    void ParentInterfaceMethod();
}

interface IMyInterface : IParentInterface
{
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
        iImp.ParentInterfaceMethod();
    }

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }

    public void ParentInterfaceMethod()
    {
        Console.WriteLine("ParentInterfaceMethod() called.");
    }
}

The example output is:

MethodToImplement() called.
ParentInterfaceMethod() called.
❮ Csharp Preprocessor Directives Csharp Namespace ❯