473,322 Members | 1,703 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,322 software developers and data experts.

When to Qualify Interface Method Names in an Implementation class

Consider this simple example:

interface IReader
{
bool Read( );
};

class MyReader : IReader
{
bool Read( ); // or should it be bool IReader.Read( ) ?
};

When I declare the implementation of the Read( ) method, what syntax should
I be using - the interface-qualified method name, or not.

I seem to have found situations where the interface-qualified name was not
permitted (can't remember the exact circumstances). It's a pity because the
interface-qualified names really improve the readability of classes which
implement interfaces.

Can anyone state the rules when the qualification is necessary, and when it
will and won't work?

Thanks
Jan 20 '06 #1
2 1602
I've done some further reading on the subject, and it is not so much that
the interface-qualified name was not an issue, but how other code referenced
the interface member implementations.

According to the Microsoft documentation, an explicit interface member
implementation (using their terminology) cannot be called from the class
reference directly, only via its interface. So there is an immediate to not
qualify the member functions unless you really have to. Of course this
depends on whether the class is likely to be called on those interface
methods directly, or whether the functions will really only be called via
their interface (and/or whether you *only* want them being called via the
interface).

an issue that the "qualification was
"Kevin Frey" <ke**********@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Consider this simple example:

interface IReader
{
bool Read( );
};

class MyReader : IReader
{
bool Read( ); // or should it be bool IReader.Read( ) ?
};

When I declare the implementation of the Read( ) method, what syntax
should I be using - the interface-qualified method name, or not.

I seem to have found situations where the interface-qualified name was not
permitted (can't remember the exact circumstances). It's a pity because
the interface-qualified names really improve the readability of classes
which implement interfaces.

Can anyone state the rules when the qualification is necessary, and when
it will and won't work?

Thanks

Jan 20 '06 #2

"Kevin Frey" <ke**********@hotmail.com> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Consider this simple example:

interface IReader
{
bool Read( );
};

class MyReader : IReader
{
bool Read( ); // or should it be bool IReader.Read( ) ?
};

When I declare the implementation of the Read( ) method, what syntax
should I be using - the interface-qualified method name, or not.

I seem to have found situations where the interface-qualified name was not
permitted (can't remember the exact circumstances). It's a pity because
the interface-qualified names really improve the readability of classes
which implement interfaces.


Not really - when you qualify them like that it is called an explicit
interface member and is not unusable without casting to the interface.

There are 3 'classic' examples:

1) Subject specific naming or implementing 'implementation' interfaces

class MyFile: IDisposable
{
public void Close()
{
// you close files don't you
}

void IDisposable.Dispose()
{
Close();
}
}

using(MyFile f = new MyFile())
{
f.Dispose(); // ERROR
f.Close(); // OK
} // f.Dispose called here automatically

2) Implementing interfaces and typesafe alternatives of the same name

class MyFile : ICloneAble
{
// unhelpfully typed version required by interface
object ICloneable.Clone()
{
// call nice version
return Clone();
}

// nicely typed version
public MyFile Clone()
{
return ...;
}
}

3) implementing multiple interfaces with name clashes

interface I1 { void F(); }
interface I2 { void F(); }
class C : I1,I2
{
void I1.F() { /* do I1 thing */ }
void I2.F() { /* do another thing */ }
}
Jan 20 '06 #3

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

Similar topics

11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
7
by: tron.thomas | last post by:
Please consider the following code: class Abstract { public: virtual ~Abstract() {} virtual void Method() = 0; }; class Concrete : public virtual Abstract
1
by: machinesofgod | last post by:
Hi, I have a class that implements the ICollection interface and subclasses the PropertyDescriptor class. The problem is that both the interface and base class are expecting an implementation of...
2
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually...
6
by: StevenECBrown | last post by:
I'm somewhat of a newbie. This question has come up for me a couple of times, and it came up again today: I have an interface IProviderFileVersionNumber that has this method called GetOrderable....
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.