Search code examples
javac#final

Can you write final functions in C#?


I wondered if you can write final (Java) functions/methods in C#. Can I see some examples of C# final functions/methods?

There is the opposite question here.


Solution

  • As opposed to Java, all methods in C# are final by default. If you want to make a method overridable, you need to mark it as "virtual".

    If a method is marked as virtual in a base class, and further down the hierarchy you want to mark it as "final", you should use the sealed keyword.