In C#, readonly struct and struct readonly

 In C#, readonly struct and struct readonly are two different features that serve different purposes. Let's explore the differences and uses of each.

readonly struct

readonly struct in C# is a struct that is immutable. This means that once an instance of the struct is created, its fields cannot be modified. Declaring a struct as readonly ensures that all fields within the struct are readonly and cannot be changed after the struct is instantiated.

Example:

public readonly struct Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Key Points:

  • All fields of a readonly struct must be immutable.
  • Properties can have getters but not setters.
  • Methods that modify the state of the struct cannot be defined.

Use Cases:

  • Use readonly struct when you want to ensure that instances of the struct are immutable.
  • Commonly used for value types that represent a single value or a small group of related values that should not change once created, such as points in a coordinate system, complex numbers, etc.

struct readonly

As of now, there is no struct readonly keyword in C#. However, you might be referring to a readonly field within a struct. A readonly field in a struct can only be assigned a value during its declaration or within the constructor of the struct, making the field immutable after the object is constructed.

Example:

public struct Point
{
    public readonly int X;
    public readonly int Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Key Points:

  • readonly fields can only be assigned in the constructor or during declaration.
  • They ensure immutability for the fields they decorate.

Use Cases:

  • Use readonly fields within a struct when you want to ensure that those specific fields are immutable after construction but still want the flexibility to modify other fields if necessary.

Comparison and Summary:

  • readonly struct: Ensures that all fields within the struct are immutable and the struct cannot have any methods that modify its state. It's a way to declare that the entire struct is immutable.
  • readonly field in a struct: Ensures that the specific field is immutable after construction, but the struct itself can still contain other mutable fields and methods that can modify the struct's state.

Here's a comparison in code:

readonly struct Example:

public readonly struct ImmutablePoint
{
    public int X { get; }
    public int Y { get; }

    public ImmutablePoint(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Struct with readonly Fields Example:

public struct PartiallyImmutablePoint
{
    public readonly int X;
    public readonly int Y;
    public int Z;

    public PartiallyImmutablePoint(int x, int y, int z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

In conclusion, use readonly struct for complete immutability of the struct, and use readonly fields within a struct when you need only certain fields to be immutable.

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form