Explicit and implicit implementation of interfaces

Source: Internet
Author: User

Explicit and implicit implementation of interfaces

When implementing interfaces, we often use class inheritance interfaces and then implement corresponding methods in interfaces. The Code is as follows:

 interface IOutput    {        void output();    }    class SimpleOutput : IOutput    {        public void output()        {            Console.WriteLine("SimpleOutput .....");        }                  }

Use the code to test the implementation result.

class Program    {        static void Main(string[] args)        {            SimpleOutput s = new SimpleOutput();            IOutput i = s;            s.output();            i.output();            Console.ReadLine();        }    }

We can see that the output s. output of the class s of the inherited interface is the same as the output I. output of the interface I.

This is a common interface implementation method. What if we do this? See the following code:

 interface IOutput    {        void output();    }    class SimpleOutput : IOutput    {        public void output()        {            Console.WriteLine("SimpleOutput .....");        }        void IOutput.output()        {            Console.WriteLine("IOutput...");        }    }
SimpleOutput inherits Ioutput and implements both the output and Ioutput. output Methods. What is the difference?

Let's use the same test code to see the result.

  class Program    {        static void Main(string[] args)        {            SimpleOutput s = new SimpleOutput();            IOutput i = s;            s.output();            i.output();            Console.ReadLine();        }    }

The result shows that the output s. output of the inherited interface s is different from the output I. output of the interface I.

S. output outputs the output method in SimpleOutput, while I. output outputs the IOutput. output method. So why?

This is actually the implicit and explicit implementation of the interface.

In the first implementation, it is a common interface implementation method. In SimpleOutput, the Output method of the interface IOutput is implicitly implemented.

In the second implementation, it is the implementation method used in some occasions. In SimpleOutput, it is explicitly implemented in the form of IOutput. output. Note: public and private modifiers cannot be added here, because the interface methods are all public by default.





What is the difference between the display implementation of interface members in C # and the implicit implementation?

If it is implicitly implemented, the implemented method belongs to the implemented class and can be accessed directly through the class object. If it is explicitly implemented, the method belongs to the interface and can be seen as implemented in the class, to access these methods, you must first convert the object to an interface object, and then call the interface object. For example, Int32 explicitly implements the IConvertible interface, when calling the ToSingle method of the IConvertible interface, you must first convert the Int32 object to the IConvertible interface object. For example:
Int32 x = 5;
Single s = (IConvertible) x). ToSingle (null );
In general, explicit implementation is rarely used, but it must be used to implement two interface methods with the same name and signature.

In C #, what is explicit and implicit execution of interfaces )? And the different implementation methods of these two interfaces, and the use

I would like to explain the interface implementation as follows:
Suppose you have set an Interface I1; there is a method to implement, such as int func1 ();
Further, you have written a Class C1 to implement this interface I1. There are two implementation methods:
Method 1: display implementation,
Public int func1 (){.....}
Method 2: implicit implementation,
Private int func1 () {...} (or protected int func1 (){.....})

The difference is found. If it is displayed, you can use C1.func1 (); to execute this function, namely:
C1 myC1 = new C1 ();
MyC1.func1 ();

But this is not implicit, because the private (or protected) modifier does not allow direct access, but you can still access this function through the interface, that is:
I1 myI1 = new C1 (); // note that myI1 is an interface and implements this interface through C1.
MyI1.func1 (); // an implicit function is accessed through an interface.

This is what you are talking about.
Hope to help you

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.