Why is the FxCop rule CA1061 a bad idea?
The docs state that this rule should not be suppressed. If I have class like so:
public class Set<T>
{ List<T> m_backingList;
public bool Contains(T value)
{
return m_backingList.Contains(value);
}
}
then I add a specific implementation like this:
public class CaseInsensitiveSet : Set<String>
{
public bool Contains(object value)
{
string stringValue = value as string;
if (stringValue == null)
return false;
return base.Contains(stringValue);
}
}
the FxCop complains, but I’m not certain why this is such a bad idea. Is there some problem I don’t see with this implementation?
The rule states why you’re getting the message:
In your child class, the Contains method takes an
objectwhich is more weakly typed thanstringand therefore hides the parent.The reason you’re getting the warning from FxCop is that this might not be an intentional design choice (since you’re not overriding anything or using the
newkeyword).Even if it is an intentional design choice, I would argue that it’s not necessarily a good one. If you already know that the collection is going to contain strings and nothing else, why would you provide a
Containsmethod that takes anything other than astring? It may appear that you’re adding flexibility into the design but, in the end, you’re really only going to confuse other developers.There are also other naming options instead of calling the method
Containswhich wouldn’t hide (intentionally or not) the base Contains method.