473,395 Members | 1,936 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,395 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 2642
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.