I have been trying to wrap my head around this FXCop violation "DoNotDeclareReadOnlyMutableReferenceTypes"
MSDN: http://msdn.microsoft.com/en-us/library/ms182302%28VS.80%29.aspx
Code from MSDN which would cause this violation:
namespace SecurityLibrary
{
public class MutableReferenceTypes
{
static protected readonly StringBuilder SomeStringBuilder;
static MutableReferenceTypes()
{
SomeStringBuilder = new StringBuilder();
}
}
}
From Jon's answer here and here , I understand that the field holding the reference to the object (in this case SomeStringBuilder) is readonly and not the object itself (which is created by new StringBuilder()
)
So taking this example, how would I change the object itself, once the field has a reference to it ? I like Eric Lippert's example of how the readonly array can be changed, and would like to see something similar for any other mutable reference type
The reference itself is not immutable. Fundamentally, it must always be known whether a type is a value or a reference, else you cannot generate the correct code to access it.
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.
Use the readonly keyword when you are not sure whether the value of a variable of an object needs to change but you want to prevent other classes from changing the value. Use the static keyword when you want the member of a class to belong to the type rather than to the instance of the type.
In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.
readonly means you can't change the reference post-construction.
The official FXCop stance is that it recommends that only types that can't be modified should be declared readonly
. Therefore something like a string
is okay because the value of the object can't be changed. However the value of StringBuilder
can changed but making it readonly only prevents you from assigning the field to a different StringBuilder
instance or null
after the constructor runs.
I disagree with FXCop on this rule. As long as one understands that this is simply an enforcement that the reference may not change post-construction then there is no confusion.
Note that value-types are made immutable by the readonly
keyword but reference types are not.
namespace SecurityLibrary
{
public class MutableReferenceTypes
{
static protected readonly StringBuilder SomeStringBuilder;
static MutableReferenceTypes()
{
// allowed
SomeStringBuilder = new StringBuilder();
}
void Foo()
{
// not allowed
SomeStringBuilder = new StringBuilder();
}
void Bar()
{
// allowed but FXCop doesn't like this
SomeStringBuilder.AppendLine("Bar");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With