Interface Name Collisions

  • Thread starter Thread starter Walid
  • Start date Start date
W

Walid

Hey,

I was trying some code with the .Net framework 1.1, and I found that the
Interface name collisions is still not resolved in that version of the .net
framework.
I am refering to that piece of code which i took from the book of Inside C#
(Archer).

using System;

interface ISerializable
{
void SaveData();
}

interface IDataStore
{
void SaveData();
}

class Test : ISerializable, IDataStore
{
public void SaveData()
{
Console.WriteLine("Test.SaveData called");
}
}

class NameCollisions1App
{
public static void Main()
{
Test test = new Test();

Console.WriteLine("Calling Test.SaveData()");
test.SaveData();
}
}

the code is still compiling, although logically it shouldn't.
So My question is Why this is not resolved ?
and is there an apparent benefit from this capability that i am not aware
of?

regards
 
Hi Walid,

If explicit interfaces are not used, the same SaveData()
implementation is used for both the interfaces. I think this feature
is in place in order to get a default implementation
to service both the interfaces - This can be used when
ISerializable.SaveData() and IDataStore.SaveData() intend to represent
the same semantics.

If teh SaveData for each of the interfaces intend to convey different
semantics,
you may like to actually use explicit interfaces to define
separate implementations for each of the SaveData() methods.

class Test : ISerializable, IDataStore
{
void ISerialzable.SaveData()
{
// Implement ISerializable.SaveData
}
void IDataStore.SaveData()
{
// Implement IDataStore.SaveData
}

}

Regards,
Aravind C
 
Hi Aravind C,

Thnx But why this is allowed to compile ?
(refering to the non referenced method name)
shouldn't be a compile error?
regards
 
Walid said:
Thnx But why this is allowed to compile ?
(refering to the non referenced method name)
shouldn't be a compile error?

No. In some cases it may be exactly what you want. Is there something
in the spec which makes you think it *should* be a compile error?
 
How can be exactly what the client wants since he doesn't know which method
has been called?
 
Walid said:
How can be exactly what the client wants since he doesn't know which method
has been called?
Casting.
The client should, if it is concerned with which version of the method is
called, cast to the right interface.
using IDataStore, ISerializable like the earlier examples were:

class Class : IDataStore, ISerializable
{
public void SaveData()
{

}

void IDataStore.SaveData()
{

}

void ISerializable.SaveData()
{
}
}
IDataStore isd = (IDataStore)Class;
ISerializable iserial = ISerializable(Class);

ids.SaveData() // calls IDataStore.Save()
iserial.SavData() // calls ISerializable.Save();
Class.SaveData() // calls the public method save data, be it an interface
method or just a plain public method.
 
Walid said:
How can be exactly what the client wants since he doesn't know which method
has been called?

If there's only one method, and that method satisfies the contract of
the interface, then why would he care? Why should someone have to
specify two methods which do the same thing, just to satisfy two
different interfaces which use the same method name?

Only if the interface names clash *and* the semantics of the interfaces
require different implementations is there a problem, which is
"solved" by explicit interface implementation. (Personally I'm not a
fan of EII in the first place, but there we go. I can see how it's
useful for solving this rare problem, but it seems it's often used when
it doesn't need to be.)
 
Back
Top