Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.
An extension method is actually a special kind of static method defined in a static class. To define an extension method, define a static class.
The first parameter of the extension method specifies the type on which the extension method is applicable. So the first parameter must be preceded with the this modifier.
Extension methods can be recognized in Visual Studio Intellisense as shown below
Example : In the below example I am performing multiplication on a integer by calling the Multiply() extension method. then I do a division. Here before dividing the two numbers I check if the divisor is positive by using the isPositive() extension method, then do the division by using the Division() extension method
1) static class containing Extension Methods
public static class Extensions
{
public static int Multiply(this int number, int multiplier)
{
return number * multiplier;
}
public static bool isPositive(this int number)
{
return number > 0;
}
public static double Division(this int dividend, int divisor)
{
return dividend / divisor;
}
}
2) class to test the extension methods
class ExtensionMethodsTest
{
/// <summary>
/// Usage one - Multiplication
/// </summary>
public void GetMultiplier()
{
int i = 5;
int result = i.Multiply(10);
Console.WriteLine(result);
}
/// <summary>
/// usage two - Division
/// </summary>
public void GetDivisor()
{
int dividend = 256;
int divisor = -1;
double? quotient = null;
if (divisor.isPositive())
{
quotient = dividend.Division(divisor);
}
else
{
Console.WriteLine("Divisor should be a positive number");
}
Console.ReadLine();
}
}
3) calling the ExtensionMethodsTest class
ExtensionMethodsTest test = new ExtensionMethodsTest();
test.GetMultiplier();
test.GetDivisor();
Result:
An extension method is actually a special kind of static method defined in a static class. To define an extension method, define a static class.
The first parameter of the extension method specifies the type on which the extension method is applicable. So the first parameter must be preceded with the this modifier.
Extension methods can be recognized in Visual Studio Intellisense as shown below
Example : In the below example I am performing multiplication on a integer by calling the Multiply() extension method. then I do a division. Here before dividing the two numbers I check if the divisor is positive by using the isPositive() extension method, then do the division by using the Division() extension method
1) static class containing Extension Methods
public static class Extensions
{
public static int Multiply(this int number, int multiplier)
{
return number * multiplier;
}
public static bool isPositive(this int number)
{
return number > 0;
}
public static double Division(this int dividend, int divisor)
{
return dividend / divisor;
}
}
2) class to test the extension methods
class ExtensionMethodsTest
{
/// <summary>
/// Usage one - Multiplication
/// </summary>
public void GetMultiplier()
{
int i = 5;
int result = i.Multiply(10);
Console.WriteLine(result);
}
/// <summary>
/// usage two - Division
/// </summary>
public void GetDivisor()
{
int dividend = 256;
int divisor = -1;
double? quotient = null;
if (divisor.isPositive())
{
quotient = dividend.Division(divisor);
}
else
{
Console.WriteLine("Divisor should be a positive number");
}
Console.ReadLine();
}
}
3) calling the ExtensionMethodsTest class
ExtensionMethodsTest test = new ExtensionMethodsTest();
test.GetMultiplier();
test.GetDivisor();
Result:
No comments:
Post a Comment