Having looked through the slides that Vagn pointed me to in a comment to my recent post about C# v3.0 Extension Methods I can understand, a little more, about the reasoning behind the change to the language. Given that the C# v3.0 spec contains the following warning:
“Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible. Extension members of other kinds, such as properties, events, and operators, are being considered but are currently not supported.”
Perhaps it would be a good idea to make classes have to ‘opt in’ to enable extension methods on them. A keyword, such as
extensible or whatever could be added to a class to allow the use of extension methods on it, much in the same way that
sealed can be used to prevent extension via inheritance. This would reduce the likelyhood that the feature will be missused by inexperienced programmers and would mean that classes could be ‘closed for extension’ by design. I would expect much of the .Net framework would naturally be ‘closed’, in the same way that much of it is ‘sealed’. This wouldn’t prevent the use of the feature where it was needed but might restrict the confusion that could be caused if it were overused.