views:

40

answers:

1

With the code below i get a CA2104 (DoNotDeclareReadOnlyMutableReferenceTypes) warning

public readonly ReadOnlyCollection<char> IllegalChars;

with part of the error message

change the field to one that is an immutable reference type. If the reference type 'ReadOnlyCollection' is, in fact, immutable, exclude this message.

I am sure ReadOnlyCollection is immutable but my question is is there a type can i use to not have this message appear?

+2  A: 

Yoy can rewrite the public field to a property:

public ReadOnlyCollection<char> IllegalChars { get; private set; }

or exclude the Code Analysis warning, because the collection can't be changed. The problem is that FxCop is unable to detect that the ReadOnlyCollection is actually immutable.

Steven
kind of irrelevant but if its immutable is there a reason for it to be a property?
acidzombie24
@acidzombie24: Yes, in the future you might want `IllegalChars` to do some extra work before returning the `ReadOnlyCollection<>`. You can't do that if `IllegalChars` is a field.
LukeH