471,306 Members | 857 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 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 2521
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ohad Young | last post: by
2 posts views Thread by Derrick | last post: by
5 posts views Thread by farseer | last post: by
7 posts views Thread by moondaddy | last post: by
52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
reply views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by

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.