Extension Methods:

This new C# 3.0 feature enables us to add new static methods to existing types with out touching the source files of those types. As per the code we can declare extension methods as

  • Declare the static class to hold those methods.
  • Add the method definition, while declaration add ‘this’ keyword before which are adding this extension method. i.e. for ex: Add, we added this before int, this means we added this method on int types.
  • In the first two methods, we need to pass one parameter, whereas for the third method no need to pass parameter as this syntax is used to show that this method is invoked on ‘int’ types.

public static  class Methods


        public static int Add(this int a, int b)


            return a + b;


        public static int DoubleTheSum(this int a, int b)


            return(( a.Add(b))*(a.Add(b)));


        public static int Negate(this int a)


            return (-a);




    class Program


        static void Main(string[] args)


            int a = 1;

            int b = 9;

            int c=a.Add(b);

            Console.WriteLine(“Sum : “ + c);

            Console.WriteLine(“Double of Sum : “ + a.DoubleTheSum(b));

            Console.WriteLine(“Negation of a is: “ + a.Negate());







Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s