implementing interface method, same signature, from two interfaces
-
Here's a little test for you. Does this code compile?
public interface IStorage{
int Save();
}public interface IPersist{
int Save();
}public class SaveImplementor : IStorage, IPersist{
public int Save(){
return 1;
}
}I'm implementing two interfaces which contain the same virtual method signature. Well, it seems a little odd to me. Obviously, if you want the separate implementations you have to write them explicitly. Like this:
public class SaveImplementor : IStorage, IPersist{
int IStorage.Save(){ return 1; } int IPersist.Save(){ return 2; }
}
IMPORTANT NOTE: Notice that in the first example you HAVE to include the
public
modifier on the method implementation. HOWEVER, on the second example where you explicitly implement the Interface method you CANNOT include thepublic
modifier. I'm filing this one under weird. But, I guess I accept it. I have to, or else the C# compiler doesn't accept me. :rolleyes: Answer - The first example does indeed compile. EDIT Oh, and after I posted that, I went back and new'd up aSaveImplementor()
and then I couldn't figure out how to call either of those explicit methods. Hmm... It's got me thinking now. EDIT 2 Here's the simple example that explains the explicit implementation: Explicit Interface Implementation - C# Programming Guide - C# | Microsoft Learn[^] . -
Here's a little test for you. Does this code compile?
public interface IStorage{
int Save();
}public interface IPersist{
int Save();
}public class SaveImplementor : IStorage, IPersist{
public int Save(){
return 1;
}
}I'm implementing two interfaces which contain the same virtual method signature. Well, it seems a little odd to me. Obviously, if you want the separate implementations you have to write them explicitly. Like this:
public class SaveImplementor : IStorage, IPersist{
int IStorage.Save(){ return 1; } int IPersist.Save(){ return 2; }
}
IMPORTANT NOTE: Notice that in the first example you HAVE to include the
public
modifier on the method implementation. HOWEVER, on the second example where you explicitly implement the Interface method you CANNOT include thepublic
modifier. I'm filing this one under weird. But, I guess I accept it. I have to, or else the C# compiler doesn't accept me. :rolleyes: Answer - The first example does indeed compile. EDIT Oh, and after I posted that, I went back and new'd up aSaveImplementor()
and then I couldn't figure out how to call either of those explicit methods. Hmm... It's got me thinking now. EDIT 2 Here's the simple example that explains the explicit implementation: Explicit Interface Implementation - C# Programming Guide - C# | Microsoft Learn[^] .raddevus wrote:
Oh, and after I posted that, I went back and new'd up a SaveImplementor() and then I couldn't figure out how to call either of those explicit methods. Hmm... It's got me thinking now.
((IPersist)saveImplementor).Save() // IPersist.Save()
((IStorage)saveImplementor).Save() // IStorage.Save()If I'm remembering correctly,
saveImplementor.Save()
raw won't work in the second EII example, you'd have to ALSO have apublic int Save() {}
. I believe this is because EII implementations are ad-hoc polymorphic, the implementation differs depending on the type-view/cast of the object - as opposed to the standard implicit ones which are parametric polymorphic, so its the same implementation for every type-view/cast of the object. Honestly it's been a long time since I've seen EII used because of this weird behavior. It makes it so you can have an object that seems to completely change it's class/behavior with a simple cast, AND all that new behavior is completely hidden from all other casts of the SAME object. -
Here's a little test for you. Does this code compile?
public interface IStorage{
int Save();
}public interface IPersist{
int Save();
}public class SaveImplementor : IStorage, IPersist{
public int Save(){
return 1;
}
}I'm implementing two interfaces which contain the same virtual method signature. Well, it seems a little odd to me. Obviously, if you want the separate implementations you have to write them explicitly. Like this:
public class SaveImplementor : IStorage, IPersist{
int IStorage.Save(){ return 1; } int IPersist.Save(){ return 2; }
}
IMPORTANT NOTE: Notice that in the first example you HAVE to include the
public
modifier on the method implementation. HOWEVER, on the second example where you explicitly implement the Interface method you CANNOT include thepublic
modifier. I'm filing this one under weird. But, I guess I accept it. I have to, or else the C# compiler doesn't accept me. :rolleyes: Answer - The first example does indeed compile. EDIT Oh, and after I posted that, I went back and new'd up aSaveImplementor()
and then I couldn't figure out how to call either of those explicit methods. Hmm... It's got me thinking now. EDIT 2 Here's the simple example that explains the explicit implementation: Explicit Interface Implementation - C# Programming Guide - C# | Microsoft Learn[^] .For added fun, try throwing some default interface members[^] into the mix. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Here's a little test for you. Does this code compile?
public interface IStorage{
int Save();
}public interface IPersist{
int Save();
}public class SaveImplementor : IStorage, IPersist{
public int Save(){
return 1;
}
}I'm implementing two interfaces which contain the same virtual method signature. Well, it seems a little odd to me. Obviously, if you want the separate implementations you have to write them explicitly. Like this:
public class SaveImplementor : IStorage, IPersist{
int IStorage.Save(){ return 1; } int IPersist.Save(){ return 2; }
}
IMPORTANT NOTE: Notice that in the first example you HAVE to include the
public
modifier on the method implementation. HOWEVER, on the second example where you explicitly implement the Interface method you CANNOT include thepublic
modifier. I'm filing this one under weird. But, I guess I accept it. I have to, or else the C# compiler doesn't accept me. :rolleyes: Answer - The first example does indeed compile. EDIT Oh, and after I posted that, I went back and new'd up aSaveImplementor()
and then I couldn't figure out how to call either of those explicit methods. Hmm... It's got me thinking now. EDIT 2 Here's the simple example that explains the explicit implementation: Explicit Interface Implementation - C# Programming Guide - C# | Microsoft Learn[^] .I've always understood C# to automatically "fill in" any empty method slots with methods of the same name. Contrast this with VB.NET where you must (AFAIK) always explicitly tell it which interface it implements.
Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix