Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 473
Nosrama Avatar asked Aug 25 '09 11:08

Nosrama


People also ask

Is Sealed same as final?

final : Cannot be extended further. sealed : Can only be extended by its permitted subclasses. non-sealed : Can be extended by unknown subclasses; a sealed class cannot prevent its permitted subclasses from doing this.

Is there final keyword in C#?

Java has a final keyword, but C# does not have its implementation. For the same implementation, use the sealed keyword. With sealed, you can prevent overriding of a method.

Is final in Java same as const in C?

Java final is equivalent to C++ const on primitive value types. With Java reference types, the final keyword is equivalent to a const pointer... i.e.

Is final same as const?

The only difference between the final and const keyword is that final is a runtime-constant, which in turn means that its value can be assigned at runtime instead of the compile-time that we had for the const keyword.


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

like image 56
Noldorin Avatar answered Oct 08 '22 23:10

Noldorin


It depends on the context.

  • For a final class or method, the C# equivalent is sealed.
  • For a final field, the C# equivalent is readonly.
  • For a final local variable or method parameter, there's no direct C# equivalent.
like image 29
LukeH Avatar answered Oct 09 '22 00:10

LukeH


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.

like image 52
Some guy Avatar answered Oct 08 '22 23:10

Some guy


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

As for the rest, it is complicated.

  • For static final fields, static readonly is the closest thing possible. It allows you to initialize the static field in a static constructor, which is fairly similar to static initializer in Java. This applies both to constants (primitives and immutable objects) and constant references to mutable objects.

    The const modifier is fairly similar for constants, but you can't set them in a static constructor.

  • On a field that shouldn't be reassigned once it leaves the constructor, readonly can be used. It is not equal though - final requires exactly one assignment even in constructor or initializer.

  • There is no C# equivalent for a final local variable that I know of. If you are wondering why would anyone need it: You can declare a variable prior to an if-else, switch-case or so. By declaring it final, you enforce that it is assigned at most once.

    Java local variables in general are required to be assigned at least once before they are read. Unless the branch jumps out before value read, a final variable is assigned exactly once. All of this is checked compile-time. This requires well behaved code with less margin for an error.

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.

like image 8
Vlasec Avatar answered Oct 08 '22 23:10

Vlasec