Extension Methods

Microsoft C# 3.0 / VB.NET 9.0 / CLR functionality that enables "adding methods" to existing classes -- a foundational language technology supporting LINQ (LanguageIntegratedQueryProject).

See "Extension Methods" heading under http://msdn.microsoft.com/netframework/future/linq/default.aspx?pull=/library/en-us/dndotnet/html/linqprojectovw.asp#linqprojec_topic3 (It's the next section after "Lambda Expressions and Expression Trees".)

Is...


Really, since the foo.bar() calling convention is just syntactic sugar on fooClass::bar(foo), it is a wonder that all languages based on the foo.bar() calling convention don't support the ExtensionMethod? approach (and likewise, don't support static-style calling of member methods). The only oddity is that it completely dements namespaces - but if one is already including all the namespaces into the local level, that problem goes away.

It also allows programmers to think a little more "object-orientedly" when dealing with classes that they can't modify directly. OOP developers are always taught to avoid producing functions that perform a laundry-list of operations on object X unless the function is a member of X - but in cases where X is not modifiable by the developer, that goal is unattainable.

Interesting to note that MultiMethods avoid this problem entirely, subsuming the characteristics of both ExtensionMethods and InstanceMethods?.

PrototypeBasedProgramming (and other systems in which classes are not immutable) also avoid this problem.


Should the use of extension methods to implement something like mixins be considered good or bad?

(See: http://www.zorched.net/2008/01/03/implementing-mixins-with-c-extension-methods/ or many other similar sites)


FebruaryZeroSix


EditText of this page (last edited May 20, 2009) or FindPage with title or text search