Search code examples
c#.netimmutabilityfxcopreadonly-collection

Why is ReadOnlyCollection<ReadOnlyCollection<T>> bad according to FxCop and what is the alternative when producing an immutable 2 dimensional object?


I am modifying all my code to conform to FxCop and this has meant ditching a lot of arrays, lists in favour of ReadOnlyCollection and I agree with this advice. However, when producing a

ReadOnlyCollection<ReadOnlyCollection<T>> 

to replace a two-dimensional array or

List<List<T>> I now get the 

CA1006: Do not nest generic types in member signatures

complaint. Firstly, although it looks unwieldy it doesn't appear to be very complex or difficult to understand as it is essentially an immutable List<List<T>>, which I imagine are extremely common given the drawbacks of arrays. Secondly, I cannot think of an alternative that stores two dimensional data and is immutable unless I am to create a new type especially for this.

What is best practice here please. Could it be that this FxCop rule doesn't really apply here and should be suppressed?

Many thanks.


Solution

  • The comments on this post seem to apply here as well:

    The warning is a general warning supposed to help you design a better and simpler public interface. In this case you get a warning about having a Expression> parameter in your method. However, for this method there is no point in simplifying the type and instead you should suppress the warning using an attribute or completely remove the rule from your ruleset.

    A silly example where you probably should consider following the advice of the rule is a method like this:

    public void F(Dictionary<String, List<Tuple<<String, Int32>>> dictionary);

    Martin Liversage's answer @ https://stackoverflow.com/a/14945331/1775528