RSS

Extending using Extension methods

22 Jan

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());
}
 
Leave a comment

Posted by on January 22, 2015 in .NET

 

Leave a comment