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.

  1. Readonly DateTime:

    • readonly DateTime now = DateTime.Now;: This statement declares a readonly variable now of type DateTime. It is initialized with the current date and time using DateTime.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
  2. Constant int:

    • const int Max = 100;: This statement declares a constant integer variable Max with the value 100.
    • Constants are immutable by nature, meaning their value cannot be changed during program execution.
  3. Method with in Parameter (Parameter Passing with Readonly Aliasing):

    • public void Method(in int x): This method takes an integer parameter x by reference using the in 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 of x within the method, which will result in a compile error:
    x = 5; // Compile error
  4. Method with Const Local Variable:

    • public void Method() contains a local variable sqrt5 that is declared as const double sqrt5 = 2.236;.
    • Constants declared inside methods are limited to that method's scope and cannot be modified.
  5. Property with Readonly Getter:

    • public string Key { get; }: This property defines a string property named Key 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.
  6. Readonly Struct:

    • public readonly struct Point: This struct defines a readonly struct named Point with two public read-only fields, X and Y.
    • Values of readonly structs cannot be modified after they are initialized.
  7. Struct with Readonly Property:

    • public struct Vector: This struct defines a struct named Vector with a public readonly property Length.
    • 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