Welcome to my blog!

Immutable Classes

August 25, 2021 09:09

Immutable Classes (edit)

  • C# Immutable Classes
  • C# Defensive Copying
  • C# readonly public struct

All About C# Immutable Classes (c-sharpcorner.com)

c# - Create a Defensive Copy in the Constructor - Stack Overflow

Chuyên Trang Chia Sẻ Và Học Hỏi Kiến Thức Về Dot NET Core - .NET Core VN

  • Immutable hiểu nôm na là không thể thay đổi còn mutable là có thể thay đổi.
  • Immutable và mutable thường được dùng cho class, object (Immutable đôi khi còn dùng với Collection).

C# Defensive Copying

You can Clone your professor instance.

Clone logic should be within Professor class.

You can then receive an already cloned professor instance in the Course constructor

public class Professor
 public string id {get; set; }
 public string firstName{get; set;}
 public string lastName {get; set;}

 Professor(string ID, string firstName, string lastname)
       this.id = ID;
       this.firstName = firstName;
       this.lastName = lastname;

 //This method can be either static or not
 //Please note that i do not implement the ICloneable interface. There is discussion in the community it should be marked as obsolete because one can't tell if it's doing a shallow-copy or deep-copy
 public static Professor Clone(Professor original)
   var clone = new Professor(original.id, original.firstName, original.lastName);
   return clone;

Then, when you invoke a new Course you'll do this

public Course AddCourse(string courseCode, string courseTitle, Professor original)
  var clonedProfessor = Professor.Clone(original);
  var course = new Course(courseCode, courseTitle, clonedProfessor);
  return course;

Write safe and efficient C# code

readonly public struct ReadonlyPoint3D
    public ReadonlyPoint3D(double x, double y, double z)
        this.X = x;
        this.Y = y;
        this.Z = z;

    public double X { get; }
    public double Y { get; }
    public double Z { get; }
public struct Point3D
    public Point3D(double x, double y, double z)
        _x = x;
        _y = y;
        _z = z;

    private double _x;
    public double X
        readonly get => _x;
        set => _x = value;

    private double _y;
    public double Y
        readonly get => _y;
        set => _y = value;

    private double _z;
    public double Z
        readonly get => _z;
        set => _z = value;

    public readonly double Distance => Math.Sqrt(X * X + Y * Y + Z * Z);

    public readonly override string ToString() => $"{X}, {Y}, {Z}";

C# Immutable Class Example

public class ImmutableClass {

    private String first_name;
    private String last_name;
    private Date dateOfBirth;

    public ImmutableClass(String first_name, String last_name, Date dob) {
        this.first_name = first_name;
        this.last_name = last_name;
        dateOfBirth = dob;

    public String getFirstName() {
        return first_name;

    public String getLastName() {
        return last_name;

    public Date getDateOfBirth() {
        return dateOfBirth;


Recent posts