Equivalent of Java 1.6 @Override for interfaces in C#

后端 未结 4 951
慢半拍i
慢半拍i 2021-02-13 03:19

This question gives the answer that Java\'s @Override has the C# equivalent of the override keyword on methods. However, since Java 1.6 the @Override annotation can

4条回答
  •  一整个雨季
    2021-02-13 03:41

    As stated, you cannot get that kind of control from an interface alone in C#. You could get it from an abstract class however. For the purpose of completeness, here is what you could do:

    public interface IA
    {
        void Foo();
        //void Bar(); - removed
    }
    
    public abstract class A : IA
    {
        virtual void Foo()
        { }
    
        // Removed method
        //virtual void Bar()
        //{ }
    }
    
    public class B : A
    {
        public override void Foo()
        { }
    
        //throws an error like the one you were receiving regarding no method to override.
        public override void Bar()
        { }
    }
    

提交回复
热议问题