Is there something in c# similar to java's @override annotation?

前端 未结 4 695
生来不讨喜
生来不讨喜 2021-02-18 23:48

I\'ve used the @Override in java and has come in quite handy. Is there anything similar in c#?

相关标签:
4条回答
  • 2021-02-19 00:02

    The C# compiler provides compile-time checking for method overrides, including checking if the method is actually overriding as you intended. You can indicate that a method should be overridden using the .NET override keyword.

    0 讨论(0)
  • 2021-02-19 00:06

    No, yes. It makesn o sesne to have an anotation because in C# there is an override kkeyword in the langauge and the compiler warnds if you "hide" a method. So, either you say "new" or "override" directly as part of the method.

    Java basically uses the annotation to hide a mistake in the langauge specifications.

    Please read the language specificastions for C#. Completely.

    0 讨论(0)
  • 2021-02-19 00:09

    In C#, you must use the override keyword to override functions.

    If you use override without a matching virtual or abstract base function, you'll get a compiler error.

    If you don't use override, and there is a matching base function, you'll get a compiler warning (unless you add new).

    0 讨论(0)
  • 2021-02-19 00:10

    In CSharp, override keyword meaning is totally different from the Java world. The equivalent in Csharp World is explicit implementation but it has a drawback.

    interface IShape
    {
         void Display();
    }
    
    class Square : IShape
    {
         void IShape.Display()
         {
    
         }
    }
    
    0 讨论(0)
提交回复
热议问题