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

╄→гoц情女王★ 提交于 2019-11-29 05:51:37

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...?

Use another method signature. Extension methods imply that you are extending the sealed class with new functionality and not overriding the ones already implemented.

Extension methods have "hide-by-name" semantics with instance members on a type. This means that any accessible instance member on a type will always shadow any extension methods with the same name, even if the extension method is a better fit. As a result, if an instance member is ever added to a type with the same name as an extension method, then the extension method can be rendered uncallable.

For more details, take a look at this post: Extension Methods Best Practices (Extension Methods Part 6)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!