[WEBINAR] Better to Ask Permission? Best Practices for Privacy and Security

Mutable Type

Definition - What does Mutable Type mean?

Mutable type, in C#, is a type of object whose data members, such as properties, data and fields, can be modified after its creation.

Mutable types are used in parallel applications, where the objects of mutable value type are maintained in the stack by the Common Language Runtime (CLR). This provides some optimization, which makes it faster than heap-allocated objects. To prevent synchronization errors from occurring in parallel applications due to multiple threads accessing the shared data, mutable types are used with some kind of locking mechanism.

Due to its inherent nature, mutable type data can get modified during run time, so mutable type is often used when the object contains a large amount of changeable data. Although mutable type is not as thread-safe and secure as immutable type, it is more often used with variables of value type, which get allocated in the stack, improving performance.

Techopedia explains Mutable Type

All built-in value types like int, double, etc., are mutable types and can be made immutable by adding the modifier "readonly" before the variables. If a mutable reference type is specified with a readonly modifier, the C# compiler generates a warning. By adding a readonly modifier to a field of reference type, the field cannot be replaced with another instance of the reference type, but will allow the instance data of the field to be modified through the reference type.

For example, StringBuilder is a mutable reference type in the .NET framework library, through which the string value of an instance created out of this type can be modified by appending, removing, replacing or inserting characters.

A mutable type should not be derived from an immutable type because there is a possibility of a virtual method in the derived class to override it such that the immutable members are overwritten.

The main limitation of mutable type is that while passing an object of mutable type (like struct) through assignment of a local variable or as a parameter to a method, the value of the object is transferred and not the object itself. By this, the copy of the object is mutated and not the original. This behavior may lead to unexpected bugs.

This definition was written in the context of C#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.