SoFunction
Updated on 2025-03-06

In-depth explanation of the interface in C#

definition

In C# language, the inheritance relationship between classes only supports single inheritance, while the interface is designed to implement multiple inheritance relationships. A class can implement multiple interfaces at the same time, and can also inherit other classes while implementing the interface, and can also be inherited between interfaces. Whether it represents inheritance between classes or class implementation interfaces and interfaces, it is represented by ":".

An interface defines properties, methods, and events, which are members of the interface. The interface only contains the member declaration. The definition of a member is the responsibility of the derived class. The interface provides a standard structure that the derived classes should follow. The interface defines the "what" part of the syntax contract, and the derived class defines the "how to do" part of the syntax contract.

Define interface syntax:

interface  interface name
{
Interface member;
}

Interface naming usually begins with the letter I, such as Itest.

Interface members are not allowed to use public, private, protected, and internal access modifiers, and static, virtual, abstract, and sealed modifiers are not allowed. Fields cannot be defined, and the defined method cannot contain method bodies.

Example: Define the interface of a book, including id, name, price attributes, and a method for selling price SalePrice().

using System;
 
namespace app
{
 interface IBook
 {
 int Id { get; set; }
 string Name { get; set; }
 double Price { get; set; }
 double SalePrice(int discount);
 }
}

accomplish

The implementation of an interface is similar to the inheritance syntax format of a class. It also rewrites the methods in the interface to give it specific implementation content.

Implement interface syntax:

class  Class name: Interface name
{
//Members in the class and members in the implementation interface
}

There are two ways to implement interface members: implicitly implement interface members and explicitly implement interface members. Implicit implementation of interface members is to modify all members of the interface with a public access modifier. An explicit implementation of an interface means that the member name implemented when implementing an interface is prefixed with the interface name.

The following uses implicit and explicit implementations for the interfaces of a book.

Implicit implementation:

class Book:IBook
 {
 // Implicit implementation of properties in the interface public int Id { get; set; }
 public string Name { get; set; }
 public double Price { get; set; }
 
 // Implicitly implement methods in interfaces public double SalePrice(int discount)
 {
  double salePrice = Price * discount * 0.1;
   return salePrice;
 }
 
 }

Implicit implementation to run:

class Program
{
 static void Main(string[] args)
 {
 Book book = new Book();
  = 1001;
  = "tynam";
  = 60;
 
 ("id:{0}" , );
 ("Book title:{0}" , );
 ("Pricing:{0}", );
 ("Selling price:{0}", (8));
 }
}

After running results:

id:1001
Book title: tynam
Pricing: 60
Selling price: 48

Explicit implementation:

class Book:IBook
{
 public double Price { get; set; }
 // Display properties in the implementation interface int  { get; set; }
 string  { get; set; }
 
 // Explicitly implement methods in interfaces double (int discount)
 {
 double salePrice = Price * discount * 0.1;
   return salePrice;
 }
 
}
 

Run it after explicit implementation:

class Program
{
 static void Main(string[] args)
 {
 Book book = new Book();
 IBook bookDetail = book;
  = 1001;
  = "tynam";
  = 60;
 
 ("id:{0}" , );
 ("Book title:{0}" , );
 ("Pricing:{0}", );
 ("Selling price:{0}", (8));
 }
}

After running results:

id:1001
Book title: tynam
Pricing: 60
Selling price: 48

Notice:The interface cannot be instantiated directly. Its members are implemented by any class or structure that implements the interface.

Polymorphic

The following two conditions are required to be met when implementing polymorphism using interfaces.

Define the interface and implement members in the interface using classes.

Create an instance of the interface to point to different implementation class objects.

Example: Define an interface name IBird, and define two implementation classes Phoenix and Duck to implement the members of the interface. The code is as follows.

interface IBird
 {
 void fly();
 }
 
 class Phoenix : IBird
 {
 public void fly()
 {
  ("The phoenix can fly");
 }
 }
 
 class Duck : IBird
 {
 public void fly()
 {
  ("Ducks can fly too");
 } 
 }

Execute after instantiation:

class Program
{
 static void Main(string[] args)
 {
  IBird phoenix = new Phoenix();
  ();
  IBird duck = new Duck();
  ();
 }
}

Execution results:

The phoenix can fly
Ducks fly too

Summarize

This is the end of this article about the in-depth and detailed explanation of interfaces in C#. For more detailed explanations of C# interfaces, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!