SoFunction
Updated on 2025-03-07

Detailed explanation of the examples of C# interface summary

When learning C# interfaces, we often use interfaces in programming, so what is an interface?

An interface describes a set of related functions that can belong to any class or structure, so a class or structure that implements an interface must implement the interface members specified in the interface definition.

The interface is defined using the interface keyword and can be composed of any combination of methods, properties, events, indexers, or these four member types.

Features of the interface:

1. Interfaces are similar to abstract base classes and cannot be instantiated directly; methods in interfaces are abstract methods, and any non-abstract type that implements interfaces must implement all members of the interface:

When the members of this interface are explicitly implemented, the implemented members cannot be accessed through the class instance, but can only be accessed through the interface instance.

When an implicitly implements members of the interface, the implemented members can be accessed through the class instance or through the interface instance, but the implemented members must be public.

2. The interface cannot contain constants, fields, operators, instance constructors, destructors or types, and cannot contain static members.

3. Interface members are automatically exposed and cannot contain any access modifiers.

4. The interface itself can be inherited from multiple interfaces, and classes and structures can inherit multiple interfaces, but the interface cannot inherit classes.

Why can't you specify a modifier for a method in an interface?

Methods in an interface are used to define contracts for communication between objects, and it is meaningless to specify that methods in an interface are private or protected. They default to public methods.

interface IProgram
 {
  void Fun();
 }
 class Program:IProgram
 {
   //Explanatory implementation of interface members  void ()
  {
   ("I am Fun.");
  }
  staticvoid Main(string[] args)
  {
   IProgram p =new Program(); //Declare an interface instance, but not instantiate the interface   ();
   ();
  }
 }

As mentioned above, implementation interfaces can be implemented explicitly and implicitly, so what are the advantages and disadvantages of these two implementations?

Generally speaking, when a class or structure wants to implement a single interface, implicit implementation can be used.

If a class or structure inherits multiple interfaces and has the same name member in the interface, an explicit implementation must be used. When the explicit implementation exists, the implicit implementation will fail.

interface IProgram
 {
  void Fun();
 }
 interface IAProgram
 {
  void Fun();
 }
 class Program : IProgram, IAProgram
 {
  void () //Explanatory implementation of interface IProgram  {
   ("I am IProgram Fun.");
  }
  void () //Explanatory implementation of interface IAProgram  {
   ("I am IAProgram Fun.");
  }
  //public void Fun() //implicity implementation of interface  //{
  // ("I am Program Fun.");
  //}
  staticvoid Main(string[] args)
  {
   //IProgram p = new Program();
   //();
   //IAProgram ap = new Program();
   //();
   Program pro =new Program();
   ((IProgram)pro).Fun();
   ((IAProgram)pro).Fun();
   ();
  }
 }

The result is:I am IProgram Fun.

I am IAProgram Fun.

Inheritance of interfaces:

Interface inheritance is different from class inheritance: First of all, class inheritance not only explains inheritance, but also implements inheritance; while interface inheritance only explains inheritance.

In other words, the derived class can inherit the method implementation of the base class, while the derived interface only inherits the member method description of the parent interface, but does not inherit the implementation of the parent interface.

Secondly, in C#, class inheritance only allows single inheritance, but interface inheritance allows multiple inheritance, and a child interface can have multiple parent interfaces.

Interfaces can be inherited from zero or multiple interfaces. When inheriting from multiple interfaces, use ":" followed by the inherited interface name, and use "," to separate the multiple interface names.

The inherited interface should be accessible, for example, inheritance from the private type or internal type interface is not allowed.

Interfaces are not allowed to be inherited directly or indirectly from themselves. Similar to the inheritance of a class, the inheritance of an interface also forms a hierarchy between interfaces.

interface IProgram
{
 void Fun();
}
interface IAProgram:IProgram
{ 
}
class Program : IAProgram
{
 void ()
 {
  ("I am IProgram Fun.");
 }
 staticvoid Main(string[] args)
 {
  Program pro =new Program();
  ((IAProgram)pro).Fun();
  ();
 }
}

Interface coverage:

Since the implementation of an interface has no method body, and the abstract method has no method body, how will it be executed when we call the abstract method in the implementation method of the interface?

 interface IProgram
 {
  void Fun();
 }
 abstractclass AProgram : IProgram
 {
  publicabstractvoid AFun();
  void ()
  {
   AFun();
  }
 }
 class Program:AProgram
 {
  publicoverridevoid AFun()
  {
   ("I am AProgram.");
  }
  staticvoid Main(string[] args)
  {
   IProgram pro =new Program();
   ();
   ();
  }
 }
//result:I am Aprogram.

Through the breakpoint, we can see that when executing ();, it will first jump to the interface implementation method, and then call the abstract function implementation method. After the abstract function method is implemented, it will return to the interface implementation method until the execution is completed.

What if we call virtual functions in the method that implements the interface?

interface IProgram
{
 void Fun();
}
class AProgram : IProgram
{
 publicvirtualvoid AFun() //Note that this is a virtual function {
  ("I am virtual AFun.");
 }
 void ()
 {
  AFun();
 }
}
class Program:AProgram
{
 publicoverridevoid AFun() //This is Override rewrite {
  ("I am override AFun.");
 }
 staticvoid Main(string[] args)
 {
  IProgram pro =new Program();
  ();
  ();
 }
}

At this time, we found that the order of execution is the same as in the previous example. So the result is:I am override AFun.

From this, we can continue to associate, when we change the override keyword to new? Is it the same result, or is it hidden like the example we mentioned before?

We put the aboveExamples to improve

interface IProgram
 {
  void Fun();
 }
 class AProgram : IProgram
 {
  publicvirtualvoid AFun()
  {
   ("I am virtual AFun.");
  }
  void ()
  {
   AFun();
  }
 }
 class Program:AProgram
 {
  publicnewvoid AFun()
  {
   ("I am new AFun.");
  }
  staticvoid Main(string[] args)
  {
   Program pro =new Program();
   ((IProgram)pro).Fun();
   ();
   ();
  }
 }

The result is:I am virtual AFun.

I am new AFun.

Since we have already mentioned before, we will not analyze this here. From this, we can see that using the New keyword is to hide it. When the virtual method is called in the method implemented by the interface, it is the same as the execution process of the class.

The difference between interface and abstract classes.

Interfaces are used for specifications, abstract classes are used for commonality.

Only methods, properties, events, and indexers can be declared in the interface. Abstract classes can have methods implementations or define non-static class variables.

Abstract classes are classes, so they can only be inherited by single, but interfaces can be implemented multiple at once.

Abstract classes can provide partial implementations of certain methods, but interfaces cannot.

An instance of an abstract class is given by its subclass. An instance of an interface is given by a class that implements the interface.

Add a method to the abstract class, and its subclasses will also have this method. If a new method is added to the interface, the class that implements it must be re-written (this is why an interface is a class specification).

Interface members are defined as public, but members of abstract classes can also be private, protected, internal, or protected internal members (where protected internal members can only be accessed in the application's code or derived classes).

In addition, the interface cannot contain fields, constructors, destructors, static members, or constants.

What are the similarities and differences between interfaces and classes in C#?

different:

The interface cannot be instantiated directly.

The interface does not contain the implementation of the method.

Interfaces can implement multiple inheritance, while classes can only be single inheritance.

Class definitions can be split between different source files.

same:

Interfaces, classes, and structures can be inherited from multiple interfaces.

Interfaces are similar to abstract base classes: any non-abstract type inheriting an interface must implement all members of the interface.

An interface can contain events, indexers, methods, and properties.

A class can implement multiple interfaces.

I hope this article will be helpful to you