In C #, the compiler will choose the correct method depending on the type of the variable declared, and not on the actual type stored in it.
Note that the code below declares W class and builds an instance of it. If you make a W interface and delete its declaration and construction, you get the same behavior for x and y , since the program, interface or class below for W does not matter in this case.
Let me show you the difference:
using System; namespace SO2851194 { class W { } class X : W { } class Y : W { } class Program { static void Main() { W w = new W(); X x = new X(); Y y = new Y(); doSomething(w); doSomething(x); doSomething(y); } static void doSomething(W w) { Console.Out.WriteLine("w"); } static void doSomething(X x) { Console.Out.WriteLine("x"); } } }
Here I declare three variables of type W , x and y and call doSomething , passing the three variables one by one. The output of this program:
w x w
As expected, the compiler will choose the method with the best suitable type of parameter, and in the case of the variable x it has a method that can accept an object of type x .
However, due to the inheritance of the class, we can change the declaration of variables, but save the types of objects, so we change the code as follows:
W w = new W(); W x = new X(); // Notice, changed variable type to W W y = new Y(); // but keep constructing X and Y
Now it produces:
w w w
Therefore, the fact that the variable x containing the object of type x did not take it into account, the compiler chose this method from the type of the variable, and not its contents.
In C # 4.0, you are now of type dynamic , so change the code again to:
dynamic w = new W(); dynamic x = new X(); dynamic y = new Y();
issues again:
w x w
since now the compiler generally refuses to choose any method before execution, and at runtime he sees that a variable with the name x actually contains an object of type x , and then selects a method with the best suitable parameter type.