Rule Definition
When a parameter of a method in a base class is ignored while making a 'base' call in an override, the value passed by the caller is ignored. This will lead to unexpected result.
Remediation
Pass the optional parameters
Violation Code Sample
public class BaseClass
{
int num;
public virtual void BaseMethod(int i = 1)
{
Console.WriteLine(i);
}
}
public class DerivedClass : BaseClass
{
public override void BaseMethod(int i = 1)
{
// ...
base.BaseMethod(); // Violation; caller's value is ignored
}
}
public class Program
{
static int Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.BaseMethod(7); // prints 1
}
}
Fixed Code Sample
public class BaseClass
{
int num;
public virtual void BaseMethod(int i = 1)
{
Console.WriteLine(i);
}
}
public class DerivedClass : BaseClass
{
public override void BaseMethod(int i = 1)
{
// ...
base.BaseMethod(i); // Violation Fixed
}
}
public class Program
{
static int Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.BaseMethod(7); // prints 7
}
}
Reference
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/base
https://cwe.mitre.org/data/definitions/628.html
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.