A C# extension method allows developers to extend the functionality of an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. C# extension method is a special kind of static method that is called as if it was an instance method on the extended type. In this article, we will create a class library and extend its functionality from the caller code by implementing extension methods in C#.
C# Extension Method
C# extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
Create a Class Library
Create a project Class Library project in Visual Studio.
Add the following code to Class 1. Class 1 has two methods, Display and Print.
using System;
using System.Text;
namespace ClassLibExtMethod
{
public class Class1
{
public string Display()
{
return ("I m in Display");
}
public string Print()
{
return ("I m in Print");
}
}
}Create a Caller Application
Now create a console application in Visual Studio using New Project. Select File > New > Project and select Visual C# and Console Application, as shown below.

Add the reference of the previously created class library to this project.

Use the following code and use the ClassLibExtMEthod.dll in your namespace:
using System;
using System.Text;
using ClassLibExtMethod;
namespace ExtensionMethod1
{
public static class XX
{
public static void NewMethod(this Class1 ob)
{
Console.WriteLine("Hello I m extended method");
}
}
class Program
{
static void Main(string[] args)
{
Class1 ob = new Class1();
ob.Display();
ob.Print();
ob.NewMethod();
Console.ReadKey();
}
}
}In the above code, you see a static class XX with a method, NewMethod. If you notice, the NewMethod takes Class1 as a parameter. This is how you extend an existing library and add your methods. Now, this New Method is available via the class library.
When you use Visual Studio Intellisense, you can see the extension method is represented with an arrow (different from the normal method sign) since they were instance methods on the extended type. See the figure below:

The output of the preceding program looks like the following.

Benefits of extension methods
- Extension methods allow existing classes to be extended without relying on inheritance or changing the class's source code.
- If the class is sealed, there is no concept of extending its functionality. For this, a new concept is introduced, in other words, extension methods.
- This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.
More code snippets of extension expansion methods
using System;
using System.Text;
namespace ExtensionMethod2
{
public static class ExtMetClass
{
public static int IntegerExtension(this string str)
{
return Int32.Parse(str);
}
}
class Program
{
static void Main(string[] args)
{
string str = "123456";
int num = str.IntegerExtension();
Console.WriteLine("The output using extension method: {0}", num);
Console.ReadLine();
}
}
}In the preceding program, we used an extension method IntegerExtension(), to convert a string to a numeric type.
Important points for the use of extension methods
- An extension method must be defined in a top-level static class.
- An extension method with the same name and signature as an instance method will not be called.
- Extension methods cannot be used to override existing methods.
- The concept of extension methods cannot be applied to fields, properties, or events.
- Overuse of extension methods is not a good style of programming.
Conclusion
I hope this article will help you to understand Extension Methods in C# 3.0 and above. If you want to learn more about Extension methods, here is another use case: Create and Use an Extension Method in C#


Ashutosh PuspwanPosted May 4, 2022, 12:51 AM EDT
You are still using VS 2005 great
Anuja SarnaikPosted Jul 28, 2021, 12:32 PM EDT
Very clear
Gurpreet AroraPosted Jun 30, 2021, 10:04 AM EDT
Nice article
Shivaji KakadPosted Dec 5, 2020, 2:22 PM EST
This is very informative article, thanks for sharing!
Kalu RamPosted Jun 28, 2020, 11:01 AM EDT
Great Sir !!
José LujánPosted Mar 24, 2020, 9:19 PM EDT
You are crack!
Raju PaladiyaPosted Mar 8, 2020, 9:30 PM EDT
Very good article
Yogendra SahuPosted Jan 20, 2020, 9:43 PM EST
Hey..!! Thanks, man for this knowledge
Aman singhPosted Oct 1, 2019, 2:29 AM EDT
Hi. Correct me If I am wrong. Eventually I will be having three classes. a) class A which will be extended which has a method A(). b) Static class B which will basically inject a new method B(this A a) to the class A. c) class C which will be going to use the method A() and method B(this A a) of the class A. Now if add another method C(this A a) in class B then I have to recompile it so that other classes can user that method C(this A a). But what if add method C() directly in the class A then I have to recompile it so that other classes can user that method C(). What is the use case of this extension methods in the real world projects?
Pankajkumar PatelPosted Aug 20, 2019, 7:37 PM EDT
Good article
Irshad AhmedPosted Aug 8, 2019, 9:14 PM EDT
Nice Explanation thanks
Laxmidhar SahooPosted May 5, 2019, 12:49 PM EDT
Thanks for a gond naration
Aditya KumarPosted Apr 23, 2019, 12:28 AM EDT
You cleared all my doubt related to extension method. Thank you sir.
Karan PrincePosted Apr 15, 2019, 12:20 AM EDT
Really helpful. Now I can create extension methods. Many Thanks.
Lillian PiersonPosted Apr 12, 2019, 1:35 AM EDT
It is very helpful. thanks for sharing.
MahmoudShehatta ShehattaPosted Nov 24, 2018, 10:45 AM EST
Thank you very much sir , it is a helpful article
Amarjit KumarPosted Aug 20, 2018, 4:47 AM EDT
Thanks for describing this concept in easy way.....Thanks for doing this kind of work from all the developer community...Keep on helping sir.
Prabhu NarayananPosted Jun 29, 2018, 9:22 AM EDT
Thank you so much to clear explaination
Devinder KumarPosted May 23, 2018, 3:36 AM EDT
Why we use always static modifier in extension method.
Carsen BusettePosted Apr 10, 2018, 11:42 AM EDT
Much thanks, This was explained very simple and was well understood.
Rajenthiran TPosted Apr 9, 2018, 1:49 AM EDT
Thank you so much. Its very useful.
Praveen SPosted Apr 3, 2018, 3:47 AM EDT
Expalined very well.
Piyush KumarPosted Mar 24, 2018, 2:33 AM EDT
Could you please explain why an extension method must be defined in a top-level static class?
Jitendra KumarPosted Feb 13, 2018, 11:34 PM EST
Its nice and useful
Harsh KumarPosted Feb 13, 2018, 11:29 PM EST
Nicely explained with easy to understand example.
Sanwar RanwaPosted Feb 1, 2018, 11:05 PM EST
Nice article thanks .....
Dnyaneshwar BabarPosted Nov 3, 2017, 12:53 AM EDT
Clearly understand actual concept..........keep writing!
Anjali SoniPosted Oct 6, 2017, 2:20 AM EDT
This is very useful thank you so much .....Easy Example
Amit GobarePosted Jan 19, 2017, 4:52 AM EST
Understood actual concept.....Nice Article!
khaleek ahmadPosted Apr 12, 2016, 1:50 AM EDT
Great Job!! Keep it Up.....
pagadala sreenivasPosted Nov 25, 2015, 1:56 AM EST
Neat and Clear , Thank you.
Sunder SinghPosted Sep 3, 2015, 3:09 AM EDT
Thanks...
Akash MalhotraPosted Jun 24, 2015, 1:45 AM EDT
Thanks for the Great article...very well explained
nega testacPosted Nov 5, 2014, 3:07 AM EST
Check the well explained tutorial, http://www.negablog.com/2014/11/create-custom-extension-method-in-c.html
Sandesh MittalPosted Sep 25, 2014, 5:34 AM EDT
First time heard about this concept. Well explained.
chandu kcckPosted Aug 22, 2014, 6:24 PM EDT
Clear and explained each step with relevant points, it greate article..... Thanks
Dasharath VishwakarmaPosted May 19, 2014, 4:20 AM EDT
neat and clear, useful article
Sonu GuptaPosted Oct 10, 2013, 5:55 AM EDT
Good article thanks
sanjay guptaPosted Aug 13, 2013, 10:18 PM EDT
Greate article ...Thanks ...
Mahesh AllePosted Mar 15, 2013, 3:14 AM EDT
Great article. It is very useful. Thank you for posting this article.
santhosh iypeeditedPosted Jan 21, 2012, 3:46 AM ESTEdited Jan 21, 2012, 3:49 AM EST
Very clear and explained each step with relevant points, well organized steps...well done
jayasuthanPosted Jul 15, 2011, 5:21 AM EDT
Short and Sweet