1

I have the following interface

public interface IMyInterface
{
    DataItem<decimal?> MyProperty { get; }
}

And the DataItem class looks something like this

public class DataItem<T>
{
    private readonly string _name;

    public DataItem(string name)
    {
        this._name = name;
    }

    public string Name
    {
        get
        {
            return _name;
        }
    }

    public T Value { get; set; }
}

When I run code analysis I get the warning

CA1006 Do not nest generic types in member signatures Consider a design where 'IMyInterface.MyProperty ' doesn't nest generic type 'DataItem<decimal?>'.

In the MDSN documentation I am told

Do not suppress a warning from this rule.

I am generally inclined to follow the guidelines. So how would I refactor my interface (or DataItem class) to follow these guidelines? Or is it more of a redesign than refactor?

Kevin Brydon
  • 12,524
  • 8
  • 46
  • 76
  • 1
    Personally I would suppress in this case. `DataItem` isn't really that complex. The rule was really meant to catch something like `Dictionary, Dictionary>>`. I've suppressed it when the nesting is only one level deep like your case. – juharr Apr 01 '16 at 13:00
  • 3
    Remember that Code Analysis looks at the IL in the assembly not your source code. The decimal? is c# short hand for a Nullable. So what code analysis sees is DataItem>. That nesting is what it is complaining about. I would be inclined to simply suppress this since this is one of those situations where code analysis is not smart enough to understand that is not what you see in the source code. – user957902 Apr 01 '16 at 13:02
  • But say I really wanted to follow the guidelines. What would I need to do to adhere to the guidelines? Maybe create a `NullableDataItem` class that constrains the `T` to a struct and has a `public T? Value { get; set; }` property? Or is there a better way? – Kevin Brydon Apr 01 '16 at 13:06
  • *"is there a better way?"* - try to search for error code itself. There are plenty of cases with solutions: [click](http://stackoverflow.com/q/417634/1997232), [click](https://social.msdn.microsoft.com/Forums/en-US/b26ffe68-1350-4d05-95ca-48c442a833f7/a-better-way-to-resolve-ca1006?forum=vstscode), ... – Sinatr Apr 01 '16 at 13:32

0 Answers0