Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use interface Interface in C #

Shulou Source: shulou.com Published: 2022-06-02 09:05:33 09月25日 Update

Editor to share with you how to use the interface interface in C#, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

The contact with the C # interface interface is not long, which at first feels very simple. Interface is added to the C # program, which will save a lot of manpower and material resources in the later expansion and maintenance of the program. The previous implementation can use the newly built Class:Project to constrain and invoke the interface as a whole. However, there are some restrictions when it comes to the instantiation of a class and the invocation of its properties and methods. The newly created Program in this example cannot instantiate the properties of clsVBProgramer and clsCSharpProgramer. After testing, it is found that the WriteProgram of class Project cannot output the properties of the first two.

The source code for the implementation of the C# interface API is as follows:

Using System; namespace Delegate {class DelegateSample {static void Main (string [] args) {clsProject Proj = new clsProject (); IProgrammer Program; / / call VBProgrammer Program = new clsVBProgramer (); Proj.WriteProgram (Program); / / call CSharpProgrammer Program = new clsCSharpProgramer (); Proj.WriteProgram (Program);}} / define interface interface IProgrammer {void WriteCode ();} / define VBProgrammer class class clsVBProgramer:IProgrammer {string StrVB = "VBProbramer" Public void WriteCode () {Console.WriteLine ("calling class {0}", StrVB);}} / define CSarpProgrammer class class clsCSharpProgramer: IProgrammer {string StrCS = "CsharpProgramer"; public void WriteCode () {Console.WriteLine ("calling class {0}", StrCS);}} / / define Project class for class clsProject {public void WriteProgram (IProgrammer Programmer) {Programmer.WriteCode () } these are all the contents of the article "how to use the interface Interface in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Interfaces clips attributes articles content examples programs not much manpower Prophase most for this feeling totality method time more source code material resources knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux NVidia MySQL Shulou Tech Info