C # example of Interface application,

Source: Internet
Author: User

C # example of Interface application,

Official definition: interfaces only contain signatures for methods, properties, events, and indexers.

When a class or struct implements an Interface, it must implement all the members of this Interface definition declaration. One Interface can inherit one or more Base interfaces) in a list containing the base class and interface, the base class must be placed in front of the member that can explicitly implement the interface when a class implements the interface, an explicitly implemented interface member cannot be accessed through an instance of the class and can only be accessed through an instance of the interface.

Namespace CSharpStudy {interface IAddInterface {string IAdd (string a, string B); int IAdd (int a, int B);} class Add {public string Addstr (string a, string B) {return a + B ;}// class Program: IAddInterface. The Add compilation error is in a list containing the base class and interface. The base class must be placed in the previous class Program: Add, IAddInterface {static void Main (string [] args) {Program P = new Program (); IAddInterface IP = new Program (); Console. writeLine (P. IAdd ( "Hello", "World"); Console. writeLine (IP. IAdd (1, 2); Console. writeLine (IP. IAdd ("Hello", "World"); // The interface instance can access the explicitly and implicitly implemented interface member // P. IAdd (1, 2); Compilation error. An explicitly implemented interface member cannot be accessed through an instance of the class and can only be accessed through an instance of the interface. } Public string IAdd (string a, string B) // to implement the interface implicitly, it must be public. {Return a + B;} int IAddInterface. IAdd (int a, int B) {return a + B ;}}

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.