C # interface


Release date:2023-08-31 Update date:2023-10-13 Editor:admin View counts:294

Label:

C # interface

Interface defines the syntax contract that all classes should follow when inheriting interfaces. The interface defines the “what is” part of the grammar contract, and the derived class defines the “how to do” part of the grammar contract.

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

The interface makes the class or structure that implements the interface consistent in form.

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

The interface itself does not implement any function, it just enters into a contract with the object that declares to implement the interface what behavior must be implemented.

Abstract classes cannot be instantiated directly, but allow concrete, functional classes to be derived.

Define the interface: MyInterface.cs

Interface usage interface keyword declaration, which is similar to a class declaration. The interface declaration defaults to public . Here is an example of an interface declaration:

interface IMyInterface
{
    void MethodToImplement();
}

The above code defines the interface IMyInterface . Usually the interface command begins with the letter I, and there is only one method forthis interface MethodToImplement() , no parameters and return values, but of course we can set parameters and return values as required.

It is worth noting that there is no concrete implementation of this method.

Implement the above interfaces:InterfaceImplementer.cs

Example

using System;
interface IMyInterface
{
        // Interface Members
    void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
    }
    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

InterfaceImplementer class implements the IMyInterface interface, the implementation of the interface is similar to the inheritance syntax of the class:

class InterfaceImplementer : IMyInterface

After inheriting the interface, we need to implement the method of the interface MethodToImplement() method name must match the method name defined by the interface

Interface inheritance: InterfaceInheritance.cs

The following example defines two interfaces IMyInterface and IParentInterface .

If one interface inherits from other interfaces, then the implementation class or structure needs to implement the members of all interfaces.

The following example IMyInterface inherited IParentInterface interface, so the interface implementation class must implement the MethodToImplement() and ParentInterfaceMethod() methods:

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 output result of the instance is:

MethodToImplement() called.
ParentInterfaceMethod() called.

Powered by TorCMS (https://github.com/bukun/TorCMS).