C # Syntax: Placing an Interface Name in an Implementation Declaration

I stumbled upon some interesting C # syntax that I do not know in the source code for the Composite Application Library for the WPF DelegateCommand<T> class.

There are some method declarations that have a prefix with the interface name ICommand , and they do not have the specified accessibility modifiers. For instance:

 bool ICommand.CanExecute(object parameter) { ... } 

What is called this syntax and where can I learn more about it? I assume there is an implicit public , but I cannot figure out what the advantage of specifying a class name is. I guess this might just be for the organization.

+4
source share
5 answers

When you post such a method, you say that this is an explicit implementation of the interface . You can read a good tutorial on MSDN at this link.

In addition, a comparison

+7
source

He called the Explicit Interface Implementation :

If a class implements two interfaces that contain an element with the same signature, then a member of the class will have interfaces for using this member as an implementation.

If two members of an interface do not perform the same function, however, this can lead to improper implementation of one or both of the interfaces. You can implement an interface element explicitly - creating a member of a class that is called only through an interface, and is an interface specific to this. This is achieved by naming a class member with the name of the interface and period.

Explicit Interface Tutorial

+5
source

It is called an explicit implementation of interfaces. This means that this particular CanExecute implementation will not be visible (and will not work) IF the object is not passed as ICommand.

This can be useful if you allow the class to provide different implementations for different interfaces, where method names overlap.

 public interface InterfaceOne { void SomeMethod(); } public interface InterfaceTwo { void SomeMethod(); } public class Impl : InterfaceOne, InterfaceTwo { public void InterfaceOne.SomeMethod() {Console.WriteLine("One");} public void InterfaceTwo.SomeMethod() {Console.WriteLine("Two");} } 

I personally hate this syntax. Take the OracleParameter example , which provides only an explicit implementation of ICloneable .

If you have a reference to OracleParameter, the Clone () method will not appear in intellisense and will not display when you go to the definition. However, is there a possibility of ((ICloneable)parameter).Clone() . To even know that this is possible, you can do it because you are almost stuck on google blogs.

+1
source

This is called an explicit interface implementation , and you can read about it here .

The basic idea is that these methods / properties are only available when explicitly using an interface of this type through an instance.

+1
source

This is called an explicit interface implementation, and you can read about it here .

In Visual Studio, if you inherit an interface, you can right-click the interface name in the class definition. You can click "Use Interface" or in the "Implement Interface Explicitly" submenu. This is a convenient shortcut for implementing interfaces.

0
source

Source: https://habr.com/ru/post/1299908/


All Articles