Content deleted Content added
added reference Tags: Mobile edit Mobile web edit |
m →External links: HTTP to HTTPS for Blogspot |
||
(15 intermediate revisions by 10 users not shown) | |||
Line 2:
{{essay|date=May 2013}}
{{More citations needed|date=September 2024}}
In [[object-oriented computer programming]], an '''extension method ''' is a [[Method (computer programming)|method]] added to an object after the original object was [[Compiler|compiled]]. The modified object is often a class, a prototype, or a type. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition.<ref name="ms_ext">{{cite web|url=http://msdn.microsoft.com/en-us/library/bb383977.aspx|title=Extension Methods|publisher=Microsoft|accessdate=2008-11-23}}</ref>▼
Not all languages implement extension methods in an equally safe manner, however. For instance, languages such as C#, Java (via [http://manifold.systems/docs.html#the-extension-manifold Manifold]
▲In [[object-oriented computer programming]], an '''extension method ''' is a [[Method (computer programming)|method]] added to an object after the original object was [[Compiler|compiled]]. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition.<ref name="ms_ext">{{cite web|url=http://msdn.microsoft.com/en-us/library/bb383977.aspx|title=Extension Methods|publisher=Microsoft|accessdate=2008-11-23}}</ref>
▲Not all languages implement extension methods in an equally safe manner, however. For instance, languages such as C#, Java (via [http://manifold.systems/docs.html#the-extension-manifold Manifold] or [https://projectlombok.org/features/experimental/ExtensionMethod Lombok]), and Kotlin don't alter the extended class in any way, because doing so may break [[class hierarchy|class hierarchies]] and interfere with virtual method dispatching. This is why these languages strictly implement extension methods statically and use [[static dispatch]]ing to invoke them.
==Support in programming languages==
Extension methods are features of numerous languages including [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]] via [http://manifold.systems/docs.html#the-extension-manifold Manifold] or [https://projectlombok.org/features/experimental/ExtensionMethod Lombok] or [https://github.com/rogerkeays/fluent Fluent], [[Gosu (programming language)|Gosu]], [[JavaScript]], [[Oxygene (programming language)|Oxygene]], [[Ruby (programming language)|Ruby]], [[Smalltalk]], [[Kotlin (programming language)|Kotlin]], [[Dart (programming language)|Dart]], [[VB.NET|Visual Basic.NET]], and [[Xojo]]. In dynamic languages like [[Python (programming language)|Python]], the concept of an extension method is unnecessary because classes (excluding built-in classes) can be extended without any special syntax (an approach known as "[[Monkey patch|monkey-patching]]", employed in libraries such as [[gevent]]).
In VB.NET and Oxygene, they are recognized by the presence of the "<code>extension</code>" keyword or attribute. In Xojo, the "<code>Extends</code>" keyword is used with global methods.
In C#, they
In Java
In Smalltalk, any code can add a method to any class at any time, by sending a method creation message (such as <code>methodsFor:</code>) to the class the user wants to extend.
In Ruby, like Smalltalk, there is no special language feature for extension, as Ruby allows classes to be re-opened at any time with the <code>class</code> keyword
In Swift, the <code>extension</code> keyword marks a class-like construct that allows the addition of methods, constructors, and fields to an existing class, including the ability to implement a new interface/protocol to the existing class.<ref>{{Cite web |title=Extensions — The Swift Programming Language (Swift 5.7) |url=https://docs.swift.org/swift-book/LanguageGuide/Extensions.html |access-date=2022-06-12 |website=docs.swift.org}}</ref>
Line 94:
===Productivity===
Consider for example [http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx IEnumerable] and note its simplicity - there is just one method, yet it is the basis of LINQ more or less. There are many
<syntaxhighlight lang="csharp">
Line 124:
===Conservative use===
A note should be placed on preferring extension methods over other means of achieving reuse and proper object
Extension methods might 'clutter' the automatic completion features of code editors, such as Visual Studio's IntelliSense, hence they should either be in their own namespace to allow the developer to selectively import them or they should be defined on a type that is specific enough for the method to appear in IntelliSense only when really relevant and given the above, consider that they might be hard to find should the developer expect them, but miss them from IntelliSense due to a missing using statement, since the developer may not have associated the method with the class that defines it, or even the namespace in which it lives - but rather with the type that it extends and the namespace that type lives in.
Line 131:
# Inherit the class and then implement the functionality in an instance method in the derived class.
# Implement the functionality in a static method added to a helper class.
# Use [[Object composition#Aggregation|aggregation]] instead of [[inheritance (
==Current C# solutions==
Line 150:
==Current VB.NET solutions==
In most ways, the VB.NET solution is similar to the C# solution above. However VB.NET has a unique advantage in that it allows members to be passed in to the extension by reference (C# only allows by value). Allowing for the following;
<syntaxhighlight lang="
Dim x As String = "some string value"
x.Reverse()
Line 208:
==See also==
* [[UFCS]], a way to use free functions as extension methods provided in the [[D programming language]]
* [[
* [[Anonymous type]]s
* [[Anonymous function|Lambda expressions]]
Line 221:
*[http://zielonka.codeplex.com/ Open source collection of C# extension methods libraries]. Now archived [https://archive.codeplex.com/?p=zielonka at Codeplex]
*[http://www.codedigest.com/Articles/CSHARP/357_Understanding_Extension_Methods_in_C_.aspx Extension method in C#]
*[
*[https://csharp-extension.com/ C# Extension Methods]. A collection.
*[https://www.extensionmethod.net/ extensionmethod.net Large database with C#, Visual Basic, F# and Javascript extension methods]
Line 227:
*[http://blogs.microsoft.co.il/blogs/basil/archive/2008/09/22/defining-your-own-functions-in-jquery.aspx Defining your own functions in jQuery]
*[http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394 Uniform function call syntax]
*[
*[http://manifold.systems/docs.html#the-extension-manifold Extension Methods in Java with Manifold]
*[https://projectlombok.org/features/experimental/ExtensionMethod Extension Methods in Java with Lombok]
*[https://github.com/rogerkeays/fluent Extension Methods in Java with Fluent]
*[https://kotlinlang.org/docs/reference/extensions.html Extension functions in Kotlin]
|