---
title: "Extension Method in C#"  
description: "I would like to share how to create Extension method in c# and why we need to have this method in C#."  
author: "Devesh Omar"  
published: 2014-05-12  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1396/extension-method-in-c-sharp  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Extension Method in C#

##### Introduction

I would like to share how to create [Extension method](https://www.mindstick.com/articles/22/extension-method) in c# and why we need to

have this [method in C#](https://www.mindstick.com/forum/33924/can-this-be-used-within-a-static-method-in-c-sharp).

First we will discuss the problem and then will rectify the problem using Extension

method.

##### Understanding the problem

1. Add [Class library](https://www.mindstick.com/forum/158577/what-is-the-significance-of-the-base-class-library-bcl-in-the-dot-net-framework) as per below screen. We created [Calculator](https://www.mindstick.com/articles/38/calculator-in-asp-dot-net-using-javascript) library.

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image001.png)

2. Add calculator class as per screen below.

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image002.png)

##### Code:

```
     public class Calculator        {        public int Add(int x, int y)        {            return x + y;        }        public int Subtract(int x, int y)        {            return x - y;        }        public int Divide(int x, int y)        {            return x / y;        }         }
```

##### Understanding the code

1. We created class library for calculator
2. Library contains Add, Subtract, Divide methods
3. By mistake developer forget to add Multiply method

3. Consuming Library (Creation of client)

We have added a client ([console application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) to consume this library)

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image004.png)

4. Adding refrence of Calculator library as created above.

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image006.jpg) >

5. Adding reference to class file as per screen below

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image008.jpg)

Here in code above we have created an object of Calculator class and we can see

here Multiply method is not available because developer forget to add.

So in this situation we have multiple options.

Problem

a. We can raise a request to Third party dll company to add required

[functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery)

But it is long process and it would also affect cost and delivery of project because

we have to wait until we get new dll from company

Solution

b. We can use Extension method to add multiply method of this calculator class

without modifying dll.

6. Adding Extension class

This should be static class

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image010.jpg)

7. We added Multiply method as per screen below.

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image012.jpg)

##### Sample code:

```
namespace CalculatorClient{   public static class ExtensionClass    {       public static int Multiply(this CalculatorThirdPartyLib.Calculator obj, int x, int y)       {           return x * y;       }    }}
```

8. Compile code
9. Now as per screen below we can see Multiply method is now available to

object of calculator class

![Extension Method in C#](http://www.mindstick.com/MindStickArticle/4c46f289-b268-4ca2-b856-f1844378afe9/Images/image014.jpg) >

##### Conclusion

In this article we learned how to create Extension method and why actually we

need it.

---

Original Source: https://www.mindstick.com/articles/1396/extension-method-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
