Home > Backend Development > C#.Net Tutorial > What is a C# interface

What is a C# interface

云罗郡主
Release: 2019-01-16 14:47:22
Original
20457 people have browsed it

What is a C# interface

What is a C# interface

C# interface is a reference type that specifies a set of function members without implementing the members. Other types - classes and structures can implement interfaces, and interfaces are Classes are used together to define so-called contracts. Contracts are about classes providing protocols for applications, and interfaces declare properties and methods. It is up to the class to define the exact content of the method.

Below we look at an example of the interface by changing a class in the Console application. Please note that we will not run the code because there is nothing to run using the interface.

Let us create an interface class called "Guru99Interface", then our main class will extend the defined interface, all the code needs to be written in the Program.cs file.

namespace DemoApplication
{
 interface Guru99Interface
 {
  void SetTutorial(int pID, string pName);
  String GetTutorial();
 }
 class Guru99Tutorial : Guru99Interface
 {
  protected int TutorialID;
  protected string TutorialName;
  public void SetTutorial(int pID, string pName)
  {
   TutorialID = pID;
   TutorialName = pName;
  }
  public String GetTutorial()
  {
   return TutorialName;
  }
  static void Main(string[] args)
  {
   Guru99Tutorial pTutor = new Guru99Tutorial();
   pTutor.SetTutorial(1,".Net by Guru99");
   Console.WriteLine(pTutor.GetTutorial());
   Console.ReadKey();
  }
 }
}
Copy after login

Code description

We first define an interface named "Guru99Interface". Note that the keyword "interface" is used to define the interface.

Next, we will define the methods used by the interface, in this case we define the same methods used in all, please note that the interface only declares the methods. It doesn't define code.

Then we make our Guru99Tutorial class extend the interface, this is where we write the code that defines the various methods declared in the interface, this coding achieves the purpose

and ensures that the class Guru99Tutorial , only the code necessary for "SetTutorial" and "GetTutorial" has been added, and nothing else.

It also ensures that the interface behaves like a contract and the class must adhere to the contract, so if the contract says it should have two methods named "SetTutorial" and "GetTutorial", that's what it should be.


The above is the detailed content of What is a C# interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template