Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# syntax: Placing the interface's name in the implementation's declaration

I came across some interesting C# syntax that I'm not familiar with in the source code for the Composite Application Library for WPF's DelegateCommand<T> class.

There are some method declarations which are prefixed with the ICommand interface name, and they do not have accessibility modifiers specified. For example:

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

What is this syntax called and where can I read more about it? I assume there's an implicit public, but I can't figure out what the benefit of specifying the class name is. My guess is that it might just be there for organization.

like image 382
sourcenouveau Avatar asked Feb 01 '10 01:02

sourcenouveau


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

When you place a method like this, you're saying that this is the explicit implementation of the interface. You can read a good tutorial on MSDN via that link.

Also, a comparison might be helpful for a full view of what this means.

like image 94
Nick Craver Avatar answered Oct 15 '22 21:10

Nick Craver


It's termed Explicit Interface Implementation:

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

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

Explicit Interface Implementation Tutorial

like image 22
Mitch Wheat Avatar answered Oct 15 '22 21:10

Mitch Wheat