473,224 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

implement interface explicitly

Hello!

The only reason I can see interface to be implemented explicitly is when the
a class implement two interface having the same
method signature. In all other cases I can implement interface implicitlly.
Can you agree with me about this statement ?

//Tony

Jun 27 '08 #1
5 2636
Hi,

I think you are correct.

"Tony Johansson" wrote:
Hello!

The only reason I can see interface to be implemented explicitly is when the
a class implement two interface having the same
method signature. In all other cases I can implement interface implicitlly.
Can you agree with me about this statement ?

//Tony

Jun 27 '08 #2
Tony Johansson wrote:
Hello!

The only reason I can see interface to be implemented explicitly is when the
a class implement two interface having the same
method signature. In all other cases I can implement interface implicitlly.
Can you agree with me about this statement ?

//Tony
You would also implement an interface explicitly if you don't want the
methods to be accessible directly for the method, but only when you are
referencing the method via an interface reference.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3
is when the a class implement two interface having the same
method signature
It could be any scenario where there are conflicting methods with the
same signature but different name but different meaning (for example,
it could conflict with a method from a base class), or it could be two
interfaces with different signatures that vary only in return-type [so
overloading isn't possible]. The classic example of the latter is
IEnumerable and IEnumerable<T- two parameterless GetEnumerator()
methods with different signatures.

Or just when you don't want those methods confusing the public API.
For example, IXmlSerializable, ITypedList, IListSource, etc - these
methods are so specialized that only code that cares about them needs
to see them.

Marc
Jun 27 '08 #4
Tony Johansson <jo*****************@telia.comwrote:
The only reason I can see interface to be implemented explicitly is when the
a class implement two interface having the same
method signature. In all other cases I can implement interface implicitlly.
Can you agree with me about this statement ?
Not quite. There are times when it *sort of* makes sense to implement
an interface, but not every method is actually supported properly -
they might throw NotImplementedException or something similar, for
example.

By implementing them explicitly, you will discourage a lot of uses of
those methods, guiding the caller to more appropriate ones.

As an example, List<Timplements the nongeneric ICollection interface,
which means it has to implement ICollection.IsSynchronized - but a
List<Tis never synchronized, so it *always* returns false. (Basically
synchronized collections are generally a bad idea, but that's a matter
for a different day.) The point is to steer users away from using
IsSynchronized and SyncRoot.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
If a base class implements an interface and makes the implementation
virtual, then in an extended hierarchy, it can be a real pain to find
exactly where a call on a method ends up.

interface I
{
void MyMethod();
}

public class Base : I
{

public virtual void MyMethod()
{
...
}
}

And then C inherits from Base, and D inherits from C and so on.

Well if you want to trap a call to MyMethod you can spend ages hunting
around all your classes looking for the most-derived class which overrides
MyMethod. But if you implement explicitly and call a virtual method in your
base class then you always know where to put your break point.

i.e.
public class Base : I
{

public virtual void MyMethod()
{
...
}

void I.MyMethod()
{
this.MyMethod() // It doesn't matter where MyMethod is overridden you
only need to trap here.
}
}

Adam.

"Tony Johansson" <jo*****************@telia.comwrote in message
news:Xg*****************@newsb.telia.net...
Hello!

The only reason I can see interface to be implemented explicitly is when
the
a class implement two interface having the same
method signature. In all other cases I can implement interface
implicitlly.
Can you agree with me about this statement ?

//Tony

Jun 27 '08 #6

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

Similar topics

3
by: Ohad Young | last post by:
Hi, I have an interface with an event. I'd like to explicitly implement the interface by a certain class. However, I received the following error: "An explicit interface implementation of an...
2
by: Derrick | last post by:
Is there an implement interface wizard is C#/Visual Studio? And, I have an interface, with many implentations, want to provide a Web Service implementation. What would be the standard practice? ...
5
by: farseer | last post by:
Hi, is it possible to both extend a class and implement an interface in C#? i am having a hard time figuring this out. i would like to do something like this: class MyClass extends...
3
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
7
by: moondaddy | last post by:
If I'm in a class that inherits an interface, is there a shortcut key that will write the implementation of the interface into the class? I remember seeing something like this in vb.net. ...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
13
by: Carl Johansson | last post by:
Being quite new to C#, I may have misunderstood this. If so please bear with me! As far as I can understand, any instances of a class that implements the IDisposable interface must call the...
0
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, when i implement my interface i built i get a choice of 1. Implement Inteface 2. Explicitly Implement Interface can someone please tell what the difference is? thanks, rodchar
3
by: Author | last post by:
Well, I was trying out this code snippet from http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx .. using System; using System.Collections; public class...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.