Skip to content

Gendarme.Rules.Security.ArrayFieldsShouldNotBeReadOnlyRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

ArrayFieldsShouldNotBeReadOnlyRule

Assembly: Gendarme.Rules.Security
Version: 2.10

Description

This rule warns if a type declares a public readonly array field. Marking a field readonly only prevents the field from being assigned a different value, the object itself can still be changed. This means, that the elements inside the array can still be changed.

Examples

Bad example:

class Bad {
    public readonly string[] Array = new string[] { "A", "B" };
}
HasPublicReadonlyArray obj = HasPublicReadonlyArray ();
obj.Array[0] = "B"; // valid

Good example:

class Good {
    private readonly string[] array = new string[] { "A", "B" };
    public string[] GetArray ()
    {
        return (string []) array.Clone();
    }
}
string[] obj = new HasPublicReadonlyArray ().GetArray ();
obj [0] = "B"; // valid, but has no effect on other users
Clone this wiki locally