Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If an extension method has the same signature as a method in the sealed class, what is the call precedence?

I was reading about extension methods in C# 3.0. The text I'm reading implies that an extension method with the same signature as a method in the class being extended would be second in order of execution - that is, the method in the sealed class gets called. If this is the case, how can you extend the sealed class ?

like image 326
Scott Davies Avatar asked Feb 20 '10 21:02

Scott Davies


1 Answers

Indeed, the actual method takes precedence over the extension method. And just to make it clear - "order of execution" suggests both might be called; only the original method will be invoked. Perhaps pick another name / signature; you can't use extension methods to monkey-patch, if that is your intent.

If there is some base-class / interface (that the type implements) that doesn't have this method, you could perhaps cast it to there...?

like image 162
Marc Gravell Avatar answered Oct 14 '22 04:10

Marc Gravell