473,881 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What good is Explicit Interface Implementation?

I just read the section (and did the exercise) in the C#
Step by Step book that covers Explict Interface
Implementation (where you specify in the method
implementation the specific interface that you are
implementing in the class.

Other than to resolve the problem that arises when a class
implements two interfaces with the same method signature,
what good is it?

The book says that using EII makes the method effectively
private to the class, but the example cited doesn't agree
with the sample program provided. The example says that a
method, once implemented using the "explicit" syntax must
be called using a cast.

IdentifierToken token = new IdentifierToken ();
token.Accept(vi sitor); // won't complile Accept not
accessible

instead...

((IVisitable)to ken).Accept(vis itor); // major tacky

IdentifierToken implements the IVisitable interface (which
includes Accept() ) using the explicit syntax...

IVisitable.Acce pt(IVisitor visitor) not...
Public Accept(IVisitor visitor)

However the sample program has explicitly implemented
methods, and no cast is used to call them.

So what is EII good for and how does it work?
Nov 15 '05
12 2819
100 <10*@100.com> wrote:

<snip>
Anyway, I believe that the main reason for EII is to make posible to
implement different interfaces, which have methods with the same
prototype.
Agreed. Hopefully that's a very rare event though.
All other usages is just tricks that *might* make the code more clear.


And in my view it makes the code *less* clear. Again, just a matter of
taste.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #11
100
Hi Steve,
I think that EII exist only for making posible implementing different
interfaces having methods with the same prototype. In this case making more
than one of them visible will lead to ambiguities. Furthermore, if you make
one of them visible the user of your class won't know part of which
interface is the method s/he is calling. Obviously they have different
implementation.

B\rgds
100

"Steve W." <bi********@hot mail.com> wrote in message
news:1e******** *************** *****@phx.gbl.. .
Many thanks to all responders. It's clearer now, but I do
agree with Jon that being able to ultimately call the
explicitly implemented method through casting is very
obscure. I see that multiple interfaces with the same
method signature needs a solution, but why can't we just
explicitly control the access of the implemented method if
that needs to be done?

Steve W.
-----Original Message-----
I just read the section (and did the exercise) in the C#
Step by Step book that covers Explict Interface
Implementati on (where you specify in the method
implementati on the specific interface that you are
implementing in the class.

Other than to resolve the problem that arises when a

class
implements two interfaces with the same method signature,
what good is it?

The book says that using EII makes the method effectively
private to the class, but the example cited doesn't agree
with the sample program provided. The example says that

a
method, once implemented using the "explicit" syntax must
be called using a cast.

IdentifierToke n token = new IdentifierToken ();
token.Accept(v isitor); // won't complile Accept not
accessible

instead...

((IVisitable)t oken).Accept(vi sitor); // major tacky

IdentifierToke n implements the IVisitable interface

(which
includes Accept() ) using the explicit syntax...

IVisitable.Acc ept(IVisitor visitor) not...
Public Accept(IVisitor visitor)

However the sample program has explicitly implemented
methods, and no cast is used to call them.

So what is EII good for and how does it work?
.

Nov 15 '05 #12
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Eric Gunnerson [MS] <er****@online. microsoft.com> wrote:
I tend to agree with you for many interfaces. But if the interface is for something that isn't really the main purpose of the class, you often don't want to make it visible to the user.

The Serialize function on ISerializable is a good example. I don't want it cluttering up the user model.
But my point is that it still *is* visible to the user, because the
fact that it implements ISerializable is visible.


True, though when users look through a class to find what methods to call,
they probably don't look at the interfaces it implements. Or at least I
don't.

But it is really a convenience mechanism - it doesn't prevent access, it
just reduces clutter.

It just feels really clunky to me, I'm afraid - although I can see
where it's needed for name collisions.



--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
6579
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption? cheers, reed
12
3312
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
21
13858
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
13
4281
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public abstract new System.Data.IDbConnection Connection }
10
1728
by: Martin Zenkel | last post by:
Assumed two assemblies (one C# and one C++), C++ refers to C#. The follwing code compiles and works well under VS 2002! VS 2003 C++ compiler reports the error "error 2555: 'TestNamespace::ClassB::IInterfaceB.get_Content': overriding virtual function return type differs and is not covariant from 'TestNamespace::ClassA::get_Content'" Explicit implementation of "IInterfaceA.Content" in
0
2225
by: Ken | last post by:
Hi I have a little application that does datavalidation. It supports dynamically loaded plugins (you drop a dll with a class implementing IValidator<Tin the same dir as the main application). All classes that implement this interface are shown in a drop down in a DataGridViewComboBoxColumn named Validator. By choosing the class to validate this specific data in the drop downbox IValidator<T>.Validate is called and returns true/false....
1
2765
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! Code Snippet 1:
1
3228
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource (_bindingMyClass) where I set the DataSource property to the interface IMyClass. Now when the MyClass object is instantiated I set the BindingSource to the instance of the IMyClass interface (_MyClass) _bindingMyClass.DataSource = this._MyClass;
2
1913
by: puzzlecracker | last post by:
I don't see the purpose of explicit interface implementation other than to hide its signature in the class that implements it, and, instead, write your own, perhaps with a different signature, implementation. Also, if you implement more than two interfaces with the same method, which is rarity in practice or so it seems. To me it appears a shallow end... Am I missing something? Thanks
0
9927
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11098
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10814
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7109
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.