简体   繁体   中英

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

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

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.

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

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

     }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM