You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The signature of virtual methods can be considered like a contract between the base class and its subclasses.
If the implementation of the base class does not use the most concrete type of a parameter, a subclass might.
I'm facing this issue with a specialized collection type.
I have type B inheriting from type A.
My collection type inherits from Collection<B>, and defines a virtual method that is meant to apply to an item of the collection, so of type B. However the base implementation only accesses members from type A, so rule S3242 kicks in and suggests to take A as parameter. But if I do that, I can imagine that a developer implementing a derived class will go through the first 3 or 4 stages of grief.
Repro steps
Have type B inherit from type A
Create a virtual method with a parameter of type B, but whose implementation only uses members of type A
using System;
using System.Collections.ObjectModel;
namespace NS1
{
public class A
{
public string ParentProperty { get; set; }
}
public class B : A
{
public string ChildProperty { get; set; }
}
public class MyCollection : Collection<B>
{
protected virtual string Manipulate(B item) // <- S3242 raised here
{
return item.ParentProperty;
}
}
}
Expected behavior
Rule should not raise a violation.
Actual behavior
Rule raises a violation.
Related information
SonarC# Version 7.2
Visual Studio Version 15.7
The text was updated successfully, but these errors were encountered:
Description
The signature of virtual methods can be considered like a contract between the base class and its subclasses.
If the implementation of the base class does not use the most concrete type of a parameter, a subclass might.
I'm facing this issue with a specialized collection type.
I have type B inheriting from type A.
My collection type inherits from
Collection<B>
, and defines a virtual method that is meant to apply to an item of the collection, so of type B. However the base implementation only accesses members from type A, so rule S3242 kicks in and suggests to take A as parameter. But if I do that, I can imagine that a developer implementing a derived class will go through the first 3 or 4 stages of grief.Repro steps
Have type B inherit from type A
Create a virtual method with a parameter of type B, but whose implementation only uses members of type A
Expected behavior
Rule should not raise a violation.
Actual behavior
Rule raises a violation.
Related information
The text was updated successfully, but these errors were encountered: