How do I use functions like final in java in C#?

Asked 2 years ago, Updated 2 years ago, 93 views

How do I use functions like final in java in C#?

java c# access-modifiers reserved-word

2022-09-22 16:16

1 Answers

In Java, the final is for many purposes, but in C#, it is the same as sealed and readonly.

Java public final class MyFinalClass {...} C# public sealed class MyFinalClass {...}

Java

public class MyClass
{
    public final void myFinalMethod() {...}
}

C#

public class MyClass : MyBaseClass
{
    public sealed override void MyFinalMethod() {...}
}

A notable difference between the two languages is that in Java, all non-stationary methods can be virtually overridden, but in C#, they must be expressed as override to be overridden. Therefore, when you no longer want to override, you can put sealed in front of the method.

Java public final double pi = 3.14; C# public readonly double pi = 3.14;

readonly can be assigned once more in the constructor, and const has a difference in which a value must be assigned at the time of declaration.


2022-09-22 16:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.