views:

273

answers:

2

Is it possible to override or shadow (new in C#) instance methods with extension methods?

+7  A: 

No. From MSDN:

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

Thomas
I was about to quote the exact same article ;)
Jean Regisser
+1  A: 

@Jean; Exactly.

Also, remember that Extension methods are just syntactic sugar; you're not actually extending the class, just making a static method that takes the class as an argument.

Nate Zaugg
Very good point, many people forget *how* they work.
Gregory