Example of a call method in a C # interface in a derived class and an external class

Source: Internet
Author: User
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 ();}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.