Gets a value indicating whether this type is immutable. A type is considered as immutable if its instance fields cannot be modified once an instance has been built by a constructor.
A stateless type (i.e a type without any instance field) is considered as immutable.
A type with at least one non-private instance field is considered as mutable, because such a field can be eventually modified outside the type.
A class that derives directly or indirectly from a mutable type is considered as mutable.
Enumerations, static types, types defined in third-party assemblies and delegate classes are never considered as immutable. Although these types might match the definition of immutability, considering them as immutable would introduce noise when searching for immutable types.
Particularly, classes that derive directly or indirectly from a class defined in a third-party assembly that is not the System.Object class, is never considered as immutable.
Using the readonly C# keyword (ReadOnly in VB.NET) on your own instance field is a good way to achieve immutability.
Notice that if a type T has some instance fields that are of reference type, the object pointed by such a reference might be modified without breaking the immutability of T. We are talking of shallow immutability.
It is still possible to break immutability by using the ref C# keyword (ByRef in VB.NET) to allow a tier method to modify the state of an instance field. As .NET framework designers considered that the immutability of the class System.String can be broken using advanced means such as reflection or unsafe code, you should consider that using the C# keyword ref or reflection or unsafe code are advanced means to break immutability of your own types. Immutable types are especially useful when you have to deal with multi-threading application. Indeed, accesses to instances of immutable types don’t need to be synchronized because such objects can’t be modified; hence all accesses are read-only accesses. To enforce your classes and structures immutability, see the attribute class NDepend.Attributes.ImmutableAttribute.
Requirements
Target Platforms: Windows 11, 10, 8, 7 and Linux, MacOS with net8.0, net7.0 or net6.0 installed