Perhaps, although you should be careful with overload options, it is generally recommended to avoid object types, as this often causes confusing code. You might be mistaken in the funny way that C # chooses overloads. He will choose a “closer” correspondence with types that can be indirectly applied to the “next” that has exact matches (see this question ).
Button myButton = // get button Window currentWindow = // get window // which method is called here? currentWindow.Display( myButton );
You want your code to be clear enough, especially when returning this code after a year or so, which causes overloading.
Extension methods provide a truly elegant way to extend the functionality of objects. You can add behavior that was not originally there. You should be careful with them, although they are very prone to creating confusing code. It is best to avoid the names of methods that have already been used, even if they are explicit overloads, since they do not appear after the class in intellisense.
The problem here is that the extension method can implicitly convert your button into an object and therefore selects itself as the best match, rather than the actual display method. You can explicitly call the extension method as a regular static call, but you cannot force it to call the base class method.
I would change the name of the extension method:
object somethingToMakeIntoAButton = // get object Window currentWindow = // get window // which method is called here? currentWindow.DisplayButton( somethingToMakeIntoAButton );
Then...
class WindowExtensions { public void DisplayButton(this Window window, object o) { Button button = BlahBlah(o);
Alternatively, if the second parameter of the extension method was a type that could not be implicitly converted to a Button (say, int or string ), this confusion also did not occur.
Keith
source share