Extension Methods
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.
LINQ operators like where, select etc Extension Methods. We will go in depth about LINQ operators in depth in the upcoming modules.
Rules to write extension method
a. The class to write extension method should be static.
b. The type to be extended should decorated with “this” keyword. For example if you want to extend the type “string” then it should be,
public static class ExtensionMethod
{
public static void StringExtension(this string notParam)
{
}
}
Here ‘notParam’ is not the method parameter.
Now can call the method “StringExtension” with any string type. For example ,
string myVar = null;
myVar.StringExtension();
"name".StringExtension();
We can’t call “StringExtension” with any other type because “StringExtension” is an extension method which extend the type string. If we extend the type “int” then we can call that extension method with “int” type only.
Extend custom class
Class is also a type so we can extend functionality of a class too. For example,
class MyClass
{
}
If I want to write extension method to “MyClass” then it would be,
public static class ExtensionMethod
{
public static void MyClassExtension(this MyClass notparam)
{
}
}
You can call “MyClassExtension” by creting object of “MyClass”. For example,
MyClass obj = new MyClass();
obj.MyClassExtension();
Why Extension Methods
a. If you want add a new method to a sealed class (sealed class is a class which cannot be inherited) then you can extend that class.
b. If you want add new function to an defined interface then you can extend that interface. For example here I am extending IEnumerable
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.
LINQ operators like where, select etc Extension Methods. We will go in depth about LINQ operators in depth in the upcoming modules.
Rules to write extension method
a. The class to write extension method should be static.
b. The type to be extended should decorated with “this” keyword. For example if you want to extend the type “string” then it should be,
public static class ExtensionMethod
{
public static void StringExtension(this string notParam)
{
}
}
Here ‘notParam’ is not the method parameter.
Now can call the method “StringExtension” with any string type. For example ,
string myVar = null;
myVar.StringExtension();
"name".StringExtension();
We can’t call “StringExtension” with any other type because “StringExtension” is an extension method which extend the type string. If we extend the type “int” then we can call that extension method with “int” type only.
Extend custom class
Class is also a type so we can extend functionality of a class too. For example,
class MyClass
{
}
If I want to write extension method to “MyClass” then it would be,
public static class ExtensionMethod
{
public static void MyClassExtension(this MyClass notparam)
{
}
}
You can call “MyClassExtension” by creting object of “MyClass”. For example,
MyClass obj = new MyClass();
obj.MyClassExtension();
Why Extension Methods
a. If you want add a new method to a sealed class (sealed class is a class which cannot be inherited) then you can extend that class.
b. If you want add new function to an defined interface then you can extend that interface. For example here I am extending IEnumerable
No comments:
Post a Comment