开发者

Is there any functional difference between c# sealed and Java's final keyword? [duplicate]

开发者 https://www.devze.com 2022-12-16 23:19 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: What is the equivalent of Java’s final in C#?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

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

In Java final applies to more than just a class.

So, I wonder: is there any functional difference between the two keywords?

Thank you, and sorry for a relatively noob question.

A quick Google search did n开发者_JAVA技巧ot satisfy my needs.


Java's final keyword is the equivalent of C#'s sealed, readonly, and sealed keywords.

Two of those three are somewhat different in Java and C#:

In Java, methods are virtual by default, so any method can be declared final to prevent it from being overriden. In C#, methods are not virtual by default, so the only overridden methods can be declared sealed (To prevent them from being further overridden)

In Java, any variable or field can be declared final to prevent it from being changed after initialization (And, for fields, outside the constructor). In C#, fields can be declared readonly, for the exact same effect, but local variables cannot. Note that Java has no equivalent of C#'s const keyword. (consts in C# are evaluated at compile-time, and the values are hard-coded instead of being referenced)

For classes, C#'s sealed classes and Java's final classes are exactly the same (AFAIK).


Take a look into this answer: What is the equivalent of Java’s final in C#?

0

精彩评论

暂无评论...
验证码 换一张
取 消