Detailed Explanation of C Interface Induction and Summary Example

  • 2021-12-13 09:00:11
  • OfStack

C # interface learning, in programming, we often use the interface, what is the interface?

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

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

Characteristics of the interface:

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

When you explicitly implement a member of the interface, the implemented member cannot be accessed through the class instance, but only through the interface instance.

When implicitly implementing a member of the interface, the implemented member can be accessed through either a class instance or an interface instance, but the implemented member must be public.

2. Interfaces 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. Interfaces themselves can inherit from multiple interfaces. Classes and structures can inherit from multiple interfaces, but interfaces cannot inherit from classes.

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

Methods in an interface are used to define a contract for communication between objects, and it makes no sense to specify that methods in an interface are private or protected. They default to public methods.


interface IProgram
 {
  void Fun();
 }
 class Program:IProgram
 {
   // Explicitly implement interface members 
  void IProgram.Fun()
  {
   Console.WriteLine("I am Fun.");
  }
  staticvoid Main(string[] args)
  {
   IProgram p =new Program(); // Declaration 1 Interface instances, but not instantiating the interface 
   p.Fun();
   Console.Read();
  }
 }

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

In general, when a class or structure is to implement a single interface, it can be implemented implicitly.

If a class or structure inherits multiple interfaces and the interfaces have members with the same name, explicit implementation is used. When explicit implementation exists, implicit implementation fails.


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

The results are: I am IProgram Fun.

I am IAProgram Fun.

Interface inheritance:

Interface inheritance is different from class inheritance: first, class inheritance is not only description inheritance, but also implementation inheritance; Interface inheritance only describes inheritance.

That is, a derived class can inherit the method implementation of the base class, and a derived interface inherits only the member method description of the parent interface, but not 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 inherit from zero or more interfaces. When inheriting from multiple interfaces, use ":" followed by the inherited interface name, and divide between multiple interface names with ",".

Inherited interfaces should be accessible, for example, inheriting from interfaces of type private or type internal is not allowed.

Interfaces are not allowed to inherit directly or indirectly from themselves. Like class inheritance, interface inheritance also forms a hierarchy between interfaces.


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

Overlay of interface:

Since the implementation of an interface has no method body, and the abstract method has no method body, what happens when we call the abstract method in the implementation method of the interface?


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

From the breakpoint, you can see that when pro. Fun (); First, it will jump to the implementation method of the interface, then call the implementation method of the abstract function, and when the method of the abstract function is implemented, it will return to the implementation method of the interface until the execution is completed.

What happens when we call virtual functions in methods that implement interfaces?


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

At this point, we find 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 Example 1 we talked about before?

Let's improve the above example:


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

The results are: I am virtual AFun.

I am new AFun.

As mentioned earlier, we will not analyze this here, so we can know that using New keyword is to hide it. When calling virtual methods in the methods implemented by interfaces, the execution process of classes is the same as that of classes.

The difference between interface and abstract class.

Interfaces are used for specification, and abstract classes are used for commonality.

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

Abstract classes are classes, so they can only be inherited by sheets, but interfaces can be implemented more than one at a time.

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

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

If you add a method to an abstract class, its subclasses will have this method at the same time. If a new method is added to the interface, the class that implements it will have to be rewritten (which is why the interface is a specification for a class).

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, interfaces cannot contain fields, constructors, destructors, static members, or constants.

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

Different:

You cannot instantiate an interface directly.

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 as:

Interfaces, classes, and structures can inherit from multiple interfaces.

An interface is similar to an abstract base class: any non-abstract type that inherits an interface must implement all members of the interface.

Interfaces can contain events, indexers, methods, and properties.

One class can implement multiple interfaces.

I hope this article is helpful to you


Related articles: