25May
How to implement and use Extension Methods in C#
Do you wish to extend the built-in types with new methods?
Want to extend custom types with new methods?
In C#, we can use the Extension Methods to extend both built-in types and custom types. These extension methods are introduced in C# 3.0
What are Extension Methods?
Extension methods are special kind of static methods in a static class which enables us to add new methods to built-in types and custom types without creating new derived type.
How to implement Extension Methods:
- Create a static method in a static class
- Pass the type which you want to extend, as a first parameter to method
- The first parameter should have the combination with keyword ‘this’ and ‘type’
The typical example of extension method looks like:
//Extending integer type
public static int GetSumOfX(this int i)
{
return i * (i + 1) / 2;
} |
In the above example, keyword ‘this’ determines to which type the extension method belongs. And ‘this’ keyword is must be a first parameter to extension method.

In the above example you can find ‘int’ type is extended with one more new method ‘GetSumOfX’. Similarly you can extend custom types too.
Microsoft has implemented many extension methods which are integrated as part of .NET Framework. One of the best known example for this, using IEnumerable objects. When you are working with LINQ objects, these objects has the build in methods called First<>, FirstOrDefault<>, GroupBy<>, OrderBy<>, etc. All these are the extension methods of IEnumerable object.
In the below sample codes, you can find out extending both built-in types and custom types:
public static class MyExtensions
{
//Extending integer type
public static int GetSumOfX(this int i)
{
return i * (i + 1) / 2;
}
//Extending string type
public static int GetTotalWords(this string s)
{
return s.Split(' ').Length;
}
//Extending Employee type
public static string GetFullName(this Employee e)
{
return e.GetFirstName() + " " + e.GetLastName();
}
}
public class Employee
{
public string GetFirstName()
{
return "Srinivasa Rao";
}
public string GetLastName()
{
return "Dhulipalla";
}
}
//Page_Load//Form_Load
int num = 100;
int totalSum =num.GetSumOfX(); //returns 5050
string sentence = "This is a string data type";
int totalWords = sentence.GetTotalWords(); //returns 6
Employee emp = new Employee();
string fullName = emp.GetFullName(); //returns firstName+lastName
|
In the above sample code, int type is extended with a new method to get the sum of N numbers and string type is extended with a new method to get number of words in a string. And also a custom type called Employee class is extended with a new method to get full name of employee.
Hope this helps you understanding extension methods in C#.
Related
In this blog I will be showing how to deploy a .net application on IIS server (self-hosted runner). ...
Read More >
IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...
Read More >
Synchronization meaning: when two or more components involved to perform any action, we expect these...
Read More >
Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...
Read More >
Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...
Read More >
File Upload Custom Control in WPFThis article is about Custom control in WPF, Custom controls are al...
Read More >
When you are working with certain projects which involves the Customer records, you might need to tr...
Read More >
What is Asp.Net Web API?Asp.Net Web API is a framework for building HTTP services that can be consum...
Read More >
Visual studio installation comes with the various predefined project templates, and we can use one o...
Read More >
Share