---
title: "Extension Method"  
description: "Extension method in  C# provides a way to add  our own method to class without inheriting it.All we have to do to  create extension method is to creat"  
author: "Anonymous User"  
published: 2010-07-22  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/22/extension-method  
category: ".net"  
tags: [".net"]  
reading_time: 1 minute  

---

# Extension Method

Extension method in C# provides a way to add our own method to class without inheriting it.

All we have to do to create extension method is to create our own static method and put ‘this’ keyword in front of first argument type of the method(the type that will be extended).

**Example**

Creating static method and using *this* keyword in front of int as it will be extended.

```
public static int factorial(this int x)           {            if (x<= 1)                return 1;            else if (x== 2)                return 2;            else                return x *factorial(x - 1);            }
```

\
now when we use int variable method it will be shown in list.

\

![Extension Method](https://www.mindstick.com/mindstickarticle/5229fa24-53fa-4f0c-9740-34aa9b67c663/images/03510e3e-8ddb-44a2-9d8b-7b4716524d17.png)

```
      private void btnFact_Click(object sender, EventArgs e)        {            int i;            i= Convert.ToInt32(txtNum.Text);                    //using factorial() method.            MessageBox.Show("Factorial is: " + i.factorial().ToString());        }
```

![Extension Method](https://www.mindstick.com/mindstickarticle/5229fa24-53fa-4f0c-9740-34aa9b67c663/images/1f72ac02-c4ff-4314-83b8-e2517c076826.png)\

---

Original Source: https://www.mindstick.com/articles/22/extension-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
