Home » Java » What is the equivalent of Java's final in C#?

What is the equivalent of Java's final in C#?

Posted by: admin November 2, 2017 Leave a comment

Questions:

What is the equivalent of Java’s final in C#?

Answers:

The final keyword has several usages in Java. It corresponds to both the sealed and readonly keywords in C#, depending on the context in which it is used.

Classes

To prevent subclassing (inheritance from the defined class):

Java

public final class MyFinalClass {...}

C#

public sealed class MyFinalClass {...}

Methods

Prevent overriding of a virtual method.

Java

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

C#

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

As Joachim Sauer points out, a notable difference between the two languages here is that Java by default marks all non-static methods as virtual, whereas C# marks them as sealed. Hence, you only need to use the sealed keyword in C# if you want to stop further overriding of a method that has been explicitly marked virtual in the base class.

Variables

To only allow a variable to be assigned once:

Java

public final double pi = 3.14; // essentially a constant

C#

public readonly double pi = 3.14; // essentially a constant

As a side note, the effect of the readonly keyword differs from that of the const keyword in that the readonly expression is evaluated at runtime rather than compile-time, hence allowing arbitrary expressions.

Questions:
Answers:

It depends on the context.

Questions:
Answers:

What everyone here is missing is Java’s guarantee of definite assignment for final member variables.

For a class C with final member variable V, every possible execution path through every constructor of C must assign V exactly once – failing to assign V or assigning V two or more times will result in an error.

C#’s readonly keyword has no such guarantee – the compiler is more than happy to leave readonly members unassigned or allow you to assign them multiple times within a constructor.

So, final and readonly (at least with respect to member variables) are definitely not equivalent – final is much more strict.

Questions:
Answers:

Java class final and method final -> sealed.
Java member variable final -> readonly for runtime constant, const for compile time constant.

No equivalent for Local Variable final and method argument final

Questions:
Answers:

http://en.csharp-online.net/CSharp_FAQ:_What_are_the_differences_between_CSharp_and_Java_constant_declarations

C# constants are declared using the const keyword for compile time constants or the readonly keyword for runtime constants. The semantics of constants is the same in both the C# and Java languages.

Questions:
Answers:

As mentioned, sealed is an equivalent of final for methods and classes.

As for the rest, it is complicated.

  • On a (static) constant defined in declaration, const can be considered an equivalent, provided that it is a primitive type or an immutable class.
  • On a field that shouldn’t be reassigned once it leaves the constructor, readonly can be used, but it is not equal – final requires exactly one assignment even in constructor.
  • A local variable that is supposed to be assigned only once cannot be created in C#, I suppose. If you are wondering why would anyone need it: You can declare a variable prior to some if-else, switch-case or so. By declaring it as final, you enforce that it is assigned exactly once, if not, compiler error happens. This requires well behaved code, so there are less errors.

Summed up, C# has no direct equivalent of final. While Java lacks some nice features of C#, it is refreshing for me as mostly a Java programmer to see where C# fails to deliver an equivalent.

Questions:
Answers:

sealed