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
Immutable object
(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!
=== C# === In [[C Sharp (programming language)|C#]] you can enforce immutability of the fields of a class with the <code>readonly</code> statement.<ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|239}} By enforcing all the fields as immutable, you obtain an immutable type. <syntaxhighlight lang="csharp"> class AnImmutableType { public readonly double _value; public AnImmutableType(double x) { _value = x; } public AnImmutableType Square() { return new AnImmutableType(_value * _value); } } </syntaxhighlight> C# have records which are immutable.<ref>{{cite web |title=Use record types - C# tutorial - C# |url=https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/records |website=learn.microsoft.com |access-date=23 February 2024 |language=en-us |date=14 November 2023}}</ref><ref>{{cite web |title=Records - C# reference - C# |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record |website=learn.microsoft.com |access-date=23 February 2024 |language=en-us |date=25 May 2023}}</ref> <syntaxhighlight lang="csharp"> record Person(string FirstName, string LastName); </syntaxhighlight>
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)