22 January 2015

Extending using Extension methods

If we want the new method to accept some parameters. Well to do this we can define additional parameters after the first parameter that is of the type to be extended (used with this keyword . Let define one more function in int called Multiply to see this in action.
static class MyExtensionMethods
{
public static int Multiply(this int val, int multiplier)
{
return val * multiplier; //10*2
}
}
static void Main(string[] args)
{
// Passing arguments in extension methods
int i = 10;
Console.WriteLine(i.Multiply(2).ToString());
}

No comments: