C# Interface
An interface defines a syntactic contract that all classes should follow when inheriting the interface. An interface defines a syntactic contract."What is it"In part, derived classes define the syntax contract"How to do it"part.
An interface defines properties, methods, and events, all of which are members of the interface. The interface only contains declarations of members. The definition of members is the responsibility of derived classes. The interface provides a standard structure that derived classes should follow.
An interface makes the classes or structs that implement it consistent in form.
Abstract classes are somewhat similar to interfaces, but they are mostly used only when a few methods are declared by the base class and implemented by derived classes.
The interface itself does not implement any functionality; it merely establishes a contract with the object that declares to implement the interface, specifying which behaviors must be implemented.
Abstract classes cannot be instantiated directly, but they allow deriving concrete classes with actual functionality.
Define interface: MyInterface.cs
Interface usageinterfaceKeyword declaration, which is similar to class declaration. Interface declarations are public by default. The following is an example of an interface declaration:
{
void MethodToImplement();
}
The above code defines the interface IMyInterface. Usually interface names start withIthe letter I. This interface has only one method, MethodToImplement(), with no parameters and no return value. Of course, we can set parameters and return values as needed.
It is worth noting that this method does not have a concrete implementation.
Next, let's implement the above interface: InterfaceImplementer.cs
Example
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.");
}
}
The InterfaceImplementer class implements the IMyInterface interface. Implementing an interface is similar in syntax to class inheritance:
class InterfaceImplementer : IMyInterface
After inheriting the interface, we need to implement the interface's method MethodToImplement(). The method name must match the method name defined in the interface.
Interface Inheritance: InterfaceInheritance.cs
The following example defines two interfaces, IMyInterface and IParentInterface.
If an interface inherits other interfaces, then the implementing class or struct must implement all members of the interfaces.
The following example IMyInterface inherits the IParentInterface interface; therefore, the interface implementation class must implement the MethodToImplement() and ParentInterfaceMethod() methods:
Example
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 result is:
MethodToImplement() called. ParentInterfaceMethod() called.other extensions