This article describes the call methods of C# interface in derived classes and external classes. Share it for your reference, as follows:
The C# interface is created through the interface keyword, and can include member variables such as properties, methods, etc. The derived classes of the interface can implement methods in the interface. A class can inherit multiple interfaces to implement methods in these interfaces, and an interface can also derive multiple classes. Methods in interfaces can be implemented by one or more of these classes. Methods in the interface can be called directly in the derived class of the interface.
Examples of calling in derived classes:
//interfacepublic interface IPersonalService { //Methods in the interface PersonalDTO QueryByUid(int uId); } //Interface derived classespublic class PersonalService : IPersonalService { //Implement interface method in derived classes-implemented implementation public PersonalDTO QueryByUid(int uId) { return _dal.QueryByUid(uId); } //Calling interface method in derived class public void GetInfo(int userId) { //Call method one IPersonalService p = new PersonalService(); PersonalDTO dto = (userId); //Call method two PersonalService p2 = new PersonalService(); IPersonalService p3 = (IPersonalService)p2; PersonalDTO dto = (userId); } }
When calling an interface method in an external class, the steps that refer to the namespace where the interface is located are the same as those in the derived class of the interface.
You can also call the interface after referring to the namespace where the interface is located in an external class, and declare an attribute of the interface type, as follows.
public IPersonalService pService{get;set;} public void getInfo() { (); }
For more information about C# related content, please check out the topic of this site:C# data structure and algorithm tutorial》、《Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.