11

Code analysis throws error CA1006: Do not nest generic types in member signatures whenever we define custom definitions in the interface contract. What is the best way of handling this so called design issue. Any deep thoughts on this.

Thanks for your valuable time to go through this.

Example:-

 Task<IList<Employee>> LoadAllEmployeeAsync();
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Suresh Kumar
  • 333
  • 2
  • 10
  • 1
    Have a look at these two similar questions [here](http://stackoverflow.com/questions/417634/are-there-any-good-workarounds-for-fxcop-warning-ca1006) and [here](http://stackoverflow.com/questions/3441563/alternative-to-nested-type-of-type-expressionfunct). – DaveShaw Jul 28 '14 at 07:46

1 Answers1

13

CA1006: Do not nest generic types in member signatures

I think the rule is pretty clear. However, the reasoning behind it is that whoever uses your class must undergo a complex process for instantiating the complex parameter(s) and decreases the adoption rate of new libraries.

However, if we think about it, the rule does not make much sense in this context. First of all, you have a nested complex generic return type, which might not be as bad as a similar parameter. Secondly, I don't think the rule was design for async methods.

I suggest to suppress it on the methods that exhibit this return type. Do not abuse it, so make sure to place it only on async methods and only when the return type is complex:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification="This is an async method.")]
Task<IList<Employee>> LoadAllEmployeeAsync();
Marcel N.
  • 13,726
  • 5
  • 47
  • 72
  • 11
    Obviously this should be fixed by Microsoft: in case of Task this rule should ignore one nested level of generic parameters. – Kryptos Aug 26 '15 at 14:15
  • This issue also pops up if you use `ValueTuple` in C# 7+. I don't think the message should be raised for a return type of `Task<(Int32 totalCount, List pagedResults)>`, for example. – Dai May 10 '19 at 20:15
  • 1
    Still an issue in 2021... Note that it only affects public methods. Changing to internal, if possible, also fixes this. – Andrew Roberts Feb 25 '21 at 10:52