This article mainly introduces the C # interface in the derived class and the external class call method, combined with the instance form analysis of C # Interface definition and specific use method, the need for friends can refer to the following
The example in this article describes the invocation methods of C # interfaces in derived and external classes. Share to everyone for your reference, as follows:
C # interfaces are created by the interface keyword, which can contain member variables such as properties, methods, and so on. A derived class of an interface can implement a method in an interface. A class can inherit multiple interfaces to implement methods in these interfaces, and an interface can derive methods from multiple class interfaces that can be implemented by one or more of these classes. A method in an interface can be called directly in a derived class of an interface.
To invoke an example in a derived class:
Interface public interface ipersonalservice{ //interface method personaldto querybyuid (int uId);} Interface derived class public class personalservice:ipersonalservice{ //derived class implements an interface method-implicitly implements public personaldto querybyuid (int UId) { return _dal. Querybyuid (uId); } Calling an interface method in a derived class public void GetInfo (int userId) { //Call mode one ipersonalservice p = new Personalservice (); C12/>personaldto dto = P.querybyuid (userId); Call mode two personalservice p2 = new Personalservice (); Ipersonalservice p3 = (ipersonalservice) P2; Personaldto dto = p3. Querybyuid (userId);} }
When you invoke a method of an interface in an external class, you refer to the namespace in which the interface resides before the steps are called in the same way as in the derived class of the interface.
Declaring a property of an interface type after referencing the namespace of an interface in an external class can also invoke the interface, as follows.
Public Ipersonalservice Pservice{get;set;} public void GetInfo () { pservice.getinfo ();}