What is an Extension Method ?Extension method helps you to add new methods to an existing types like int,
string or to your custom types(classes).I will explain with an example to make it more clear;Int datatype has a method called ToString().Now I want a method on Int to
find out whether it is an Odd or Even (Say IsEven()). How to do it ? Use Extension Method!
Why Extension Methods ?My first question here is how do we achieve the above task(adding IsEven()
function) without Extension Method?Yes, we can do this by Extending Int type to another class and write IsEven
function there. But in this case you have to create an object of this class
to use this new function. If you use extension methods you don't need to do
all these things!
Suppose if you have a class but no source code and want to add a new function
to this class, use Extension Methods..
How to use Extension Methods ?I will give 2 examples for this. One is adding IsEven() to the built in Int
type. In second example I will show you how to add a new method to our custom class using Extension method.
All your extension methods should be static and it should be in a static
class. Also, the first parameter of the extension method describes to which
type this method should be part of.
Adding IsEven() method to Int Type:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static bool IsEven(this int val)
{
int result= val%2;
return result == 0 ? true : false;
}
}
}
How to use it? see below code..
protected void Page_Load(object sender, EventArgs e)
{
int num = 100;
bool isEven=num.IsEven();
Response.Write(isEven.ToString());
}
Example 2: Adding method to custom type
Below is my operation class:
namespace Business{
public class operations
{
public operations()
{ }
public int Add(int num1, int num2)
{ return num1 + num2; }
}
}
Now I want to add a new method called Substract() to this class using
extension method. So here goes our Extension method:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static int Substract(this operations op, int num1, int num2)
{ return num1 - num2; }
}
}
Using this...
protected void Page_Load(object sender, EventArgs e)
{
operations obj = new operations();
int res= obj.Substract(500, 200);
Response.Write(res.ToString());
}
string or to your custom types(classes).I will explain with an example to make it more clear;Int datatype has a method called ToString().Now I want a method on Int to
find out whether it is an Odd or Even (Say IsEven()). How to do it ? Use Extension Method!
Why Extension Methods ?My first question here is how do we achieve the above task(adding IsEven()
function) without Extension Method?Yes, we can do this by Extending Int type to another class and write IsEven
function there. But in this case you have to create an object of this class
to use this new function. If you use extension methods you don't need to do
all these things!
Suppose if you have a class but no source code and want to add a new function
to this class, use Extension Methods..
How to use Extension Methods ?I will give 2 examples for this. One is adding IsEven() to the built in Int
type. In second example I will show you how to add a new method to our custom class using Extension method.
All your extension methods should be static and it should be in a static
class. Also, the first parameter of the extension method describes to which
type this method should be part of.
Adding IsEven() method to Int Type:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static bool IsEven(this int val)
{
int result= val%2;
return result == 0 ? true : false;
}
}
}
How to use it? see below code..
protected void Page_Load(object sender, EventArgs e)
{
int num = 100;
bool isEven=num.IsEven();
Response.Write(isEven.ToString());
}
Example 2: Adding method to custom type
Below is my operation class:
namespace Business{
public class operations
{
public operations()
{ }
public int Add(int num1, int num2)
{ return num1 + num2; }
}
}
Now I want to add a new method called Substract() to this class using
extension method. So here goes our Extension method:
namespace OperationsMethods
{
public static class ExtensionMethods
{
public static int Substract(this operations op, int num1, int num2)
{ return num1 - num2; }
}
}
Using this...
protected void Page_Load(object sender, EventArgs e)
{
operations obj = new operations();
int res= obj.Substract(500, 200);
Response.Write(res.ToString());
}