How to resolve the algorithm Enforced immutability step by step in the C# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Enforced immutability step by step in the C# programming language
Table of Contents
Problem Statement
Demonstrate any means your language has to prevent the modification of values, or to create objects that cannot be modified after they have been created.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Enforced immutability step by step in the C# programming language
The C# code provided focuses on the concept of immutability, which ensures that certain variables maintain their values throughout the program's execution. This code demonstrates the use of readonly and const, two modifiers that help achieve immutability.
-
Readonly DateTime:
readonly DateTime now = DateTime.Now;
: This statement declares a readonly variablenow
of typeDateTime
. It is initialized with the current date and time usingDateTime.Now
.- Once initialized, the value of the
now
variable can no longer be changed. If you try to reassign a value to it, as shown below, the compiler will raise an error:
now = DateTime.UtcNow; // Compile error
-
Constant int:
const int Max = 100;
: This statement declares a constant integer variableMax
with the value 100.- Constants are immutable by nature, meaning their value cannot be changed during program execution.
-
Method with in Parameter (Parameter Passing with Readonly Aliasing):
public void Method(in int x)
: This method takes an integer parameterx
by reference using thein
modifier.- When passing a parameter by reference with
in
modifier, the compiler creates a readonly alias for the original variable, ensuring that the original value cannot be modified within the method. The following code shows an attempt to modify the value ofx
within the method, which will result in a compile error:
x = 5; // Compile error
-
Method with Const Local Variable:
public void Method()
contains a local variablesqrt5
that is declared asconst double sqrt5 = 2.236;
.- Constants declared inside methods are limited to that method's scope and cannot be modified.
-
Property with Readonly Getter:
public string Key { get; }
: This property defines a string property namedKey
with a readonly getter.- The property can be assigned a value in the constructor or in the declaration, but it cannot be changed later on.
-
Readonly Struct:
public readonly struct Point
: This struct defines a readonly struct namedPoint
with two public read-only fields,X
andY
.- Values of readonly structs cannot be modified after they are initialized.
-
Struct with Readonly Property:
public struct Vector
: This struct defines a struct namedVector
with a public readonly propertyLength
.- The
Length
property is calculated during struct initialization and is immutable.
Source code in the csharp programming language
readonly DateTime now = DateTime.Now;
const int Max = 100;
public void Method(in int x) {
x = 5; //Compile error
}
public void Method() {
const double sqrt5 = 2.236;
...
}
public string Key { get; }
public readonly struct Point
{
public Point(int x, int y) => (X, Y) = (x, y);
public int X { get; }
public int Y { get; }
}
public struct Vector
{
public readonly int Length => 3;
}
You may also check:How to resolve the algorithm Loops/Continue step by step in the Clipper programming language
You may also check:How to resolve the algorithm Test integerness step by step in the Go programming language
You may also check:How to resolve the algorithm Boolean values step by step in the OCaml programming language
You may also check:How to resolve the algorithm Steffensen's method step by step in the Raku programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Eero programming language