Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. implementing interface method, same signature, from two interfaces

implementing interface method, same signature, from two interfaces

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharptutorialcomquestion
4 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raddevus
    wrote on last edited by
    #1

    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 the public 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 a SaveImplementor() 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[^] .

    J Richard DeemingR H 3 Replies Last reply
    0
    • R raddevus

      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 the public 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 a SaveImplementor() 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[^] .

      J Offline
      J Offline
      Jon McKee
      wrote on last edited by
      #2

      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 a public 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.

      1 Reply Last reply
      0
      • R raddevus

        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 the public 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 a SaveImplementor() 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[^] .

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • R raddevus

          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 the public 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 a SaveImplementor() 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[^] .

          H Offline
          H Offline
          honey the codewitch
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups