CA2104: Do not declare read only mutable reference types

In this article

Rule CA2104 is obsolete and will be removed in a future version of Visual Studio.

Cause

An externally visible type contains an externally visible read-only field that is a mutable reference type.

Rule description

A mutable type is a type whose instance data can be modified. The System.Text.StringBuilder class is an example of a mutable reference type. It contains members that can change the value of an instance of the class. An example of an immutable reference type is the System.String class. After it has been instantiated, its value can never change.

The read-only modifier (readonly in C#, ReadOnly in Visual Basic, and const in C++) on a reference type field (or pointer in C++) prevents the field from being replaced by a different instance of the reference type. However, the modifier does not prevent the instance data of the field from being modified through the reference type.

This rule may inadvertently show a violation for a type that is, in fact, immutable. In that case, it's safe to suppress the warning.