Jump to content
  • Advertisement
Sign in to follow this  
Dynamo_Maestro

Explicit Interface and overriding

This topic is 1845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Code Example:

 

    public interface ITest
    {
        void SomeMethod();
    }

    public abstract class TestBase : ITest
    {
        void ITest.SomeMethod()
        { 
            // Stuff
        }
    }

    /// <summary>
    /// Compiler Error: -
    ///     Error	1	'AnotherTest.ITest.SomeMethod()': containing type does not implement interface 'ITest'
    /// 
    /// Need to implement ITest to avoid this error
    /// </summary>
    public class AnotherTest : TestBase, ITest
    {
        /// <summary>
        /// Did I just override this?
        /// What if I wanted it sealed
        /// </summary>
        void ITest.SomeMethod()
        {
            // Stuff
        }
    }

 

When you explicitly implement an interface, you cant use the modifier abstract or virtual on its members you want to be called explicitly (in this cast ITest.SomeMethod()), so my question is, in the class AnotherTest did I override ITest.SomeMethod()? and why do I get a compiler error if I don't inherit ITest in AnotherTest despite it already being inherited in the base class?

 

I am not familiar with CIL or how this is treated internally, but would this be treated as a virtual method being overridden or a completely new method?

Edited by Dynamo_Maestro

Share this post


Link to post
Share on other sites
Advertisement

TestBase already inherits ITest, so you don't want to inherit ITest in AnotherTest surely?

 

Yeh that's what I thought, but it throws an error. Ofc if I don't re-implement ITest.SomeMethod() in AnotherTest, I don't have to inherit it and I get no error, I only get a compiler error (as shown in summary) IF I re-implement it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!