It is worth noting here that an overridden version is called every time. Change the override to:
public override void MyMethod(string s = "bbb") { Console.Write("derived: "); base.MyMethod(s); }
And the result:
derived: bbb derived: aaa
A method in a class can do one or two of the following:
- It defines an interface for calling another code.
- It defines the implementation that is executed upon invocation.
It may not do both, since the abstract method does only the first.
Inside the BBB call to MyMethod() calls the method defined in AAA .
Since there is an override in the BBB , a call to this method results in a call to the BBB .
Now the definition in AAA tells you the call code for two things (well, some others don't matter here either).
- Signature
void MyMethod(string) . - (For those languages ââthat support it), the default value for one parameter is
"aaa" , and therefore when compiling the code of the form MyMethod() , if the method corresponding to MyMethod() is not found, you can replace it with `MyMethod ( "aaa").
So what makes the call in the BBB : the compiler sees the call to MyMethod() , does not find the MyMethod() method, but finds the MyMethod(string) method. He also sees that in the place where it is defined, there is a default value of "aaa", so at compile time it changes this to a call to MyMethod("aaa") .
Inside the BBB , AAA is considered the place where the AAA methods are defined, even if they are overridden in the BBB so that they can be overloaded.
At run time, MyMethod(string) is called with the argument "aaa". Since there is an overridden form called a form, but it is not called with "bbb" because this value has nothing to do with the runtime implementation, but with the definition of compilation time.
Adding this. changes whose definition is checked, and therefore changes which argument is used in the call.
Edit: why it seems to me more intuitive.
Personally, and since I'm talking about what is intuitive, it can only be personal, I find it more intuitive for the following reason:
If I were BBB encoding, then if I would call or redefine MyMethod(string) , I would think of it as "doing AAA stuff" - it BBB take "do AAA stuff", but it's all doing AAA . Therefore, whether it be a call or an override, I will know that it was the AAA that defined MyMethod(string) .
If I were to call code that used BBB , I would think about âusing BBB stuffâ. Perhaps I do not know very well what was originally defined in AAA , and I would think of it as simplicity of implementation (if I did not use the AAA interface as well).
The behavior of the compiler is consistent with my intuition, so when I first read the question, it seemed to me that Mono had an error. After reviewing, I donât see how either the specified behavior performs better than the other.
Nevertheless, despite the fact that, remaining on a personal level, I will never use additional parameters with abstract, virtual or overridden methods, and if I redefine someone else, I would compare them.