HomeTHỦ THUẬT - PHẦN MỀMInterface c# là gì

Interface c# là gì

10:54, 17/03/2021

A C# Interface is the definition of a class methods, properties, and its members. In this article, you'll learn what C# interfaces are & how to use C# interfaces with real world code example.




Bạn đang xem: Interface c# là gì

This article explains the following:

Why lớn use an interface in C#? What is an interface? What is the best example of an interface?

Why to lớn use an interface

I hope everyone is quite familiar with OOPhường concepts in C++. (Class và Object, Inheritance và its type, so on). Inheritance allows creating classes that are derived from other classes, so that they automatically include some of its "parent"s" members, plus its own. The following are types of inheritances.

Multi-Level Inheritance


classFather //FatherRelations--Members classMother:Father //MotherRelations--Members classChild:Mother//Multi-LevelInheritance //AccessingbothRelationsclassmembers.
classFather //FatherRelations--Members classMother //MotherRelations--Members classChild:Mother,Father//MultipleInheritance //AccessingbothRelationsclassmembers.

This type of multiple inheritance is possible in C++ but it"s not possible in C#.

*

What is Interface?

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by structs và classes, that must provide an implementation for each interface thành viên declared. So, what are interfaces good for if they don"t implement functionality? They"re great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component khổng lồ expose specific public members that will be used in a certain way. Because interfaces must be implemented by derived structs & classes, they define a contract.




Xem thêm: Làm Sao Chép Nhạc Vào Iphone

interfaceIMyInterface voidMethodToImplement();//AbstractMethodsignature. classInterfaceImplementer:IMyInterface staticvoidMain() InterfaceImplementerobj=newInterfaceImplementer(); obj.MethodToImplement(); publicvoidMethodToImplement() //AbstractMethodImplementation

Here, we advised to lớn use "I" as the prefix for the interface to lớn understand that the interface is an interface.

Best example for Interface

Here I have the best example for understanding interfaces. Not Possible:

*
Open a Console Application & give "InterFaceDemo" as the project name, then add a new class nhà cửa và rename it "ODDEVEN.CS". ODDEVEN.cs


usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceInterFaceDemo interfaceIOne voidONE();//PureAbstractMethodSignature interfaceITwo voidTWO(); interfaceIThree:IOne voidTHREE(); interfaceIFour voidFOUR(); interfaceIFive:IThree voidFIVE(); interfaceIEVEN:ITwo,IFour classODDEVEN:IEVEN,IFive//MustImplementalltheabstractmethod,inDerivedclass. publicvoidONE()//ImplementationofAbstractMethod. Console.WriteLine("ThisisONE"); publicvoidTWO() Console.WriteLine("ThisisTWO"); publicvoidTHREE() Console.WriteLine("ThisisTHERE"); publicvoidFOUR() Console.WriteLine("ThisisFOUR"); publicvoidFIVE() Console.WriteLine("ThisisFIVE");


Xem thêm: Lỗ Hổng Zero Day Attack Là Gì, Cuộc Tấn Công Khai Thác Lỗ Hổng Zero

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceInterFaceDemo classProgram staticvoidMain(string<>args) Console.WriteLine("ThisisODD"); IFiveobj1=newODDEVEN(); obj1.ONE(); obj1.THREE(); obj1.FIVE(); Console.WriteLine(" ThisisEVEN"); IEVENobj2=newODDEVEN(); obj2.TWO(); obj2.FOUR(); Console.ReadLine();

Chuyên mục: THỦ THUẬT - PHẦN MỀM