Then I started searching on the web about it. I found new exciting feature of framework 3.0, which is “Extension Methods”. You can extend existing classes without inheritance using extension methods. There are some which you should keep in mind while writing extension method.
- You cannot override existing method by writing extension method.
- If extension method has same name as instance method then it will not called, because the priority of instance method is high. At compile time extension method has low priority.
- You can write extension for Properties, data Members and events.
Here is an example to use extension method. Include the namespace and call your extension method like the original method of the instance.
namespace ExtensionTest
{
public static class ExtensionOfString
{
public static bool IsNumber(this string value)
{
Regex expression =
new Regex(@"^[0-9]([\.\,][0-9]*)?$");
return expression.IsMatch(value);
}
}
}
using ExtensionTest;You can see how easy it is to write extension method in Dot net.
. . . . .
string number = "4";
bool isNumber = number.IsNumber();


