What are extension methods in .NET?
EDIT: I have posted a follow up question at Usage of Extension Methods
Extension methods comprise the communication techniques between extension workers and target groups. To facilitate farmers' decisions whether or not and how to adopt fish farming. the problems to be solved. Generally speaking, mass media help extension agents to reach large numbers of farmers simultaneously.
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.
An extension method must be defined in a top-level static class. An extension method with the same name and signature as an instance method will not be called. Extension methods cannot be used to override existing methods. The concept of extension methods cannot be applied to fields, properties or events.
To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.
Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.
Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages.
Reference: http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx
Here is a sample of an Extension Method (notice the this
keyword infront of the first parameter):
public static bool IsValidEmailAddress(this string s) { Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); return regex.IsMatch(s); }
Now, the above method can be called directly from any string, like such:
bool isValid = "[email protected]".IsValidEmailAddress();
The added methods will then also appear in IntelliSense:
(source: scottgu.com)
As regards a practical use for Extension Methods, you might add new methods to a class without deriving a new class.
Take a look at the following example:
public class Extended { public int Sum() { return 7+3+2; } } public static class Extending { public static float Average(this Extended extnd) { return extnd.Sum() / 3; } }
As you see, the class Extending
is adding a method named average to class Extended
. To get the average, you call average
method, as it belongs to extended
class:
Extended ex = new Extended(); Console.WriteLine(ex.average());
Reference: http://aspguy.wordpress.com/2008/07/03/a-practical-use-of-serialization-and-extension-methods-in-c-30/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With