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

↘锁芯ラ 提交于 2019-12-06 17:16:28

问题


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


回答1:


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.




回答2:


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).




回答3:


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()
     {

     }
}



回答4:


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.



来源:https://stackoverflow.com/questions/6025776/is-there-something-in-c-sharp-similar-to-javas-override-annotation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!