Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Constant/immutable parameters ==== In Java, it is possible to prevent reassignment of a local variable or method parameter by using the {{Java|final}} keyword. Applying [[final (Java)#Final variables|this keyword]] to a primitive type variable causes the variable to become immutable. However, applying {{Java|final}} to a reference type variable only prevents that another object is assigned to it. It will not prevent the data contained by the object from being mutated. As of {{not a typo|C#7}}, it is possible to prevent reassignment of a method parameter by using the {{mono|in}} keyword, however this keyword cannot be used on local variables. As with Java, applying {{mono|in}} to a parameter only prevents the parameter from being reassigned to a different value. It is still possible to mutate the data contained by the object.<ref name="in">{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier |title=in parameter modifier (C# Reference) |date=5 March 2018 |publisher=Microsoft |archive-url=https://archive.today/20190126124708/https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier |url-status=live |archive-date=26 January 2019 |quote=The in keyword causes arguments to be passed by reference. It is like the ref or out keywords, except that in arguments cannot be modified by the called method. |access-date=26 January 2019 }}</ref> {| class="wikitable" |- ! Java !! C# |- | <syntaxhighlight lang="java"> public int addOne(final int x) { x++; // ERROR: a final variable cannot be reassigned return x; } public List addOne(final List<Integer> list) { list.add(1); // OK: it is still possible to modify a // final (reference type) variable return list; }</syntaxhighlight> || <syntaxhighlight lang="csharp"> public int AddOne(in int x) { x++; // ERROR: a readonly parameter cannot be reassigned return x; } public List<int> AddOne(in List<int> list) { list.Add(1); // OK: it is still possible to modify a // readonly (reference type) parameter return list; } </syntaxhighlight> |} Both languages do not support essential feature of [[const-correctness]] that exists in [[C (programming language)|C]]/[[C++]], which makes a method constant. Java defines the word "constant" arbitrarily as a {{Java|static final}} field. As a convention, these variable names are capital-only with words separated with an [[underscore]] but the Java language doesn't insist on this. A parameter that is only {{Java|final}} is not considered as a constant, although it may be so in the case of a [[primitive data type]] or an [[immutable class]], like a {{Java|String}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)