Today I was writing a small program to understand the fundamentals of Optional Parameters of C#.
Following is the program:
abstract class AbstractClass
{
internal abstract void Test();
}
sealed class DerivedClass : AbstractClass
{
internal override void Test()
{
Console.WriteLine("In override DerivedClass.Test() method");
}
internal void Test(int a = 1)
{
Console.WriteLine("In DerivedClass.Test(int a=1) method " + a);
}
internal void Test(int b, int a = 1)
{
Console.WriteLine("In DerivedClass.Test(int b, int a=1) method " + string.Format("{0} {1}", b, a));
}
}
This is how I called Test()
method:
DerivedClass d = new DerivedClass();
d.Test();
d.Test(6);
d.Test(b:7);
Output :
In DerivedClass.Test(int a=1) method 1
In DerivedClass.Test(int a=1) method 6
In DerivedClass.Test(int b, int a=1) method 7 1
Regarding d.Test();
: Here my understanding is, it will treat Test()
as method with an optional parameter, and will invoke Test(int a = 1)
with this output:
In DerivedClass.Test(int a=1) method 1
But this is what confuses me when executing d.Test(6);
: Why this method call isn't giving output as:
In DerivedClass.Test(int b, int a=1) method 6 1
As per my understanding "6" is the mandatory parameter and it should invoke
internal void Test(int b, int a = 1)
Kindly explain what is wrong with my understanding.
Also how to call an overriden method?
internal override void Test()
The rules around optional parameters can be a little confusing, but a simple rule of thumb to remember is that it'll always use a method with less parameters in preference to one with more.
So for
the method with just one parameter:
will be used, even though it's an optional parameter.