Extension Methods: How to use, what is possible and what is not By Diablo iii Printer Friendly Version View My Articles 20 Points |  |
A brief article on the usage of Extension methods. A new feature that is made available in .Net 3.0 and above. |
Using Extension methods to really extend .Net and other Types.
Extension methods are new additions to .Net Framework. They help you to extend the exisiting objects by attaching the new ones. The basic idea of this concept is to extend the method for all the instances of the particular type that is extended.
This can be better visualized through an example as follows:
Every string will have this extension method, hereafters.
namespace Utilities.Extensions
{
public static class StringExtensions
{
public static bool IsValidEmailID(this string value, string emailId)
{
//call some custom validation for the email or use Regex.
string pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" return Regex.IsMatch(emailAddress, pattern);
}
}
}
The "this" keyword is the type that is being extended. In the above example, it is the "string" type.
And we can use it for all string variables in the project hereafter. To use the extended method, follow the example as below.
string emailID = "user@gmail.com";
//returns whether it is a valid email id or not.
bool isValidEmailID = emailID.IsValidEmailId(@emailID);
Please note:
1. The class with the Extended methods has to exisit in the same project.
2. The Class with the Extended methods has to be static and also the method has to defined as Static.
3. The first parameter of the Method should be the "this" keyword, that indicates the type that has to be extended, the second is the input to be tested.
4. Extension methods cannot be used to override existing methods
5. The concept of extension methods cannot be applied to fields, properties or events
Every parameter after the "this" keyword is the parameter to the extended function. There are cases when you need to pass more than one parameter to the extended funcion. Just add another parameter after the "this" keyboard.
You could even decide the return type of the Extended method.
There are many more complex situations, when these extended methods turn out to be handy. Extension methods not only help to cut the code short, and make the implementation generic and uniform but also helps to encapsulate references.
For e.g. You can do stuff like formatting text with an extension method.
code snippet as follows:
public static string FormatText(this string text, bool ensurePeriodAtEnd)
{
if (text == null)
return string.Empty;
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text == "\"\"")
return string.Empty;
if (string.StartsWith("\""))
text = text.Substring(1);
else if (string.EndsWith("\""))
text = text.Substring(0, text.Length - 1);
text = text.Trim();
if (ensurePeriodAtEnd && !text.EndsWith("."))
text += ".";
return text;
}
The following methods extends the ListControl to have a SelectAll method.
public static void SelectAll(this ListControl control)
{
foreach (ListItem item in control.Items)
item.Selected = true;
}
Extension methods also allow to avoid the maintenance of code. They can be implemented on objects that are sealed or non-inheritable. |
| |
| Article Discussion: Extension Methods: How to use, whatz possible and whatz not |
| Diablo iii posted at 23-Oct-08 11:36 |
| Original Article |
 |
|