Saturday 21 April 2012


Extention Methods In .Net


You want to create Method inside any of the system class or your custom class without creating a new derived type, recompiling, or otherwise modifying the original type/class.
Here is solution ...
Create extention method.
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
See below screen shot :
extention.PNG
you can try it with string.
As its not possible to override string class and its methods, the reasonbeing that String is defined to be a Sealed class. You can extend it using extention method.
Refer this MSDN artical for more details: http://msdn.microsoft.com/en-us/library/bb383977.aspx.

No comments:

Post a Comment