3

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.

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
Pedruski
  • 41
  • 4
  • Non-opinion based portion of your question is already answered on the site ([CA1006](http://stackoverflow.com/questions/14945199/do-not-nest-generic-types-in-member-signatures)). You may also consider new immutable types as alternative - http://stackoverflow.com/questions/210428/are-immutable-arrays-possible-in-net which would be better choice (as ReadonlyCollection are actually not immutable - http://stackoverflow.com/questions/32438988/readonlycollection-are-the-objects-immutable). – Alexei Levenkov Jun 03 '16 at 15:38
  • 1
    Does the user of your type really expect to use it as a collection-of-collections, or do they expect to use it as a collection that requires two indices? The latter seems more likely; consider making your own collection type that uses a "nested" collection like this as an implementation detail, not as the public interface. – Eric Lippert Jun 03 '16 at 16:51
  • @EricLippert The user wants a collection with two indices. The syntax of an array is much simpler than that of a collection of collections and given that it cannot be changed in any way I can't see any advantages of the latter. I don't think an array like this exists, which surprises me as I would of thought that lots of other developers would have travelled down the same path that I am on of a) Passing immutable arrays to eliminate the unexpected. b) Being thread safe c) Reducing change and therefore complexity d) Adhering to FxCop. I think my best option is ImmutableList>. – Pedruski Jun 04 '16 at 13:26

1 Answers1

-1

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

Community
  • 1
  • 1
Tyler Lee
  • 2,736
  • 13
  • 24
  • Please vote/flag to close as duplicate if you believe exact answer to another question is enough. Posting whole answer (even with attribution) is not a good practice. – Alexei Levenkov Jun 03 '16 at 15:35
  • if you think thats the answer then mark this question as duplicate. – M.kazem Akhgary Jun 03 '16 at 15:35
  • @Tyler Thanks for that. Is ReadOnlyCollection> a common construct because I haven't found many examples of it being used but I would have thought that it would be very common when immutability is desired. – Pedruski Jun 03 '16 at 15:38
  • @Pedruski i think thats not common... jagged arrays are more common... `T[][]` – M.kazem Akhgary Jun 03 '16 at 15:39
  • I read Martin's answer before posting but thought that my question was different as it was with regards to ReadOnlyCollection> which I still think must be a very common signature but I found very little about on stackoverflow or the internet generally. I also read about the new immutable collections but didn't think they applied as they would also be ImmutableList> which would receive the same error in FxCop. Thanks for your input. I understand if you wish to mark this as a duplicate. – Pedruski Jun 03 '16 at 15:56
  • For the record I will continue to use ReadOnlyCollection> and suppress the warnings in FxCop. – Pedruski Jun 03 '16 at 15:57