In this post, I will explain about What is Extension Methods in .net?. An extension method is a method which used to extend a type, such as string or integer, without recompiling or modifying the original type. In essence, they are a type of static method, but they are called as if the method is native to the type. Extension methods are available from the 3.5 version of the .NET Framework and can be implemented on any type in the .NET Framework or any custom type that you define.An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviours to an existing type without altering. In the extension method, "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by the extension method.
An extension method having the same name and signature like as an instance method will never be called since it has low priority than an instance method. The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in the same class file using directives. The compiler will cause an error if you will try to call one of them extension method. An extension method is only called when there is no native method found.
Definition of extension method
Public Static class <class_name>
{
Public static <return_type> <method_name>(this <type> <type_obj>)
{
}
}
0 comments:
Post a Comment
Please do not enter any spam link in the message box.