include::../description.adoc[]

== How to fix it

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class BaseClass
{
    public virtual void MyMethod(int i = 1)
    { 
        Console.WriteLine(i);
    }
}

public class DerivedClass : BaseClass
{
    public override void MyMethod(int i = 1)
    {
        // ... 
        base.MyMethod(); // Noncompliant: caller's value is ignored
    }

    static int Main(string[] args) 
    {
        DerivedClass dc = new DerivedClass();
        dc.MyMethod(12);  // prints 1
    }
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
public class BaseClass
{
    public virtual void MyMethod(int i = 1)
    { 
        Console.WriteLine(i);
    }
}

public class DerivedClass : BaseClass
{
    public override void MyMethod(int i = 1)
    {
        // ... 
        base.MyMethod(i);
    }

    static int Main(string[] args) 
    {
        DerivedClass dc = new DerivedClass();
        dc.MyMethod(12);  // prints 12
    }
}
----

== Resources

=== Documentation

Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments[Optional Arguments]

include::../rspecator.adoc[]
