Introduction : A extension methods is an important method of c#. An extension method is always used in a static class. It is
parametric type. If you want to not create the object of a class and invoke the
methods without a class object in this type situation we have to used the
extension methods. In a extension methods we have invoke methods of a
class to it's variable. The first variable of extension methods is always
used of "this" keywords. The benefit of extension
methods you have no need to create the object of a class and invoke of methods
of class to it's variable.
Step 1 : Open Visual Studio 2010.
- Go to file -> New->Projects.
- Open Windows Application Form.

Step 2 : In a window form add four buttons.
- Add
- Sub
- Mul
- Div

Step 3 : In Solution Explorer right click and add a new class.
- Solution Explorer->Right Click->Add ->New
Item's-> add class.

code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Extension_methods
{
static
class manish
{
public static
int sum(this
int a, int b)
{
int c = a + b;
return c;
}
public static
int mul(this
int a, int b)
{
int c = a * b;
return c;
}
public static
int sub(this
int a, int b)
{
int c = a - b;
return c;
}
public static
int div(this
int a, int b)
{
int c = a / b;
return c;
}
}
}
Step 5 : In window form Invoke a
methods on button click event.


Join the conversation! Your thoughts help the community grow.