Today I wrote a small program to understand the basics of advanced C # options.
The 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 what I called the Test() method:
DerivedClass d = new DerivedClass(); d.Test(); d.Test(6); d.Test(b:7);
Output:
In the DerivedClass.Test method (int a = 1) 1
In the DerivedClass.Test method (int a = 1) 6
In the DerivedClass.Test method (int b, int a = 1) 7 1
Regarding d.Test(); : Here, I understand that it will treat Test() as a method with an optional parameter and will call Test(int a = 1) with this output:
In the DerivedClass.Test method (int a = 1) 1
But this is what confuses me when doing d.Test(6); : why this method call does not give an output like:
In the DerivedClass.Test method (int b, int a = 1) 6 1
According to my understanding, "6" is a required parameter, and it should call
internal void Test(int b, int a = 1)
Please explain what is wrong with my understanding.
Also how to call overriden method?
internal override void Test()