Developers are always looking for creative ways to extend existing libraries and classes that were developed by other developers. Let’s say, in one of my projects, I use a class library that was developed by a developer that I have no idea about and obviously, I do not have access to the original code. Now, I want to extend the functionality of the class library (class or struct) by adding some methods. The straight-forward OOP method is to inherit a class from an existing class and extend its methods. What if we just want to extend an existing type by just a method? The inheritance can be used but it has some limitations. The first problem is that structs don’t fall into this category. Secondly, inheritance is not an effective way to extend an existing class by just a method or two. The C# compiler does much more internally when a class is being inherited. What about if a class is a sealed class? Many developers won’t let you inherit a type from their types and they define their classes as sealed classes.
An alternative approach is to define extension methods. Using extension methods, we can add new methods to existing types and use them through the type instances.
The Extension methods feature in C# gives developers power to add new methods to existing types without inheriting the type, recompiling, or modifying it.
Let’s use an example.
Let’s say, we have a class called AuthorElite defined in the following code snippet.
- public class AuthorElite
- {
- public ArrayList AllEliteAuthors()
- {
- return new ArrayList()
- {
- "Mahesh Chand", "Praveen Kumar", "Raj Kumar"};
- }
- }
Now, this class can be in the same or a different assembly. As long as you have added a reference to the assembly, you’re good to go.
The way to extend the preceding class is by creating a new static class and defining a new static method within the new class. The new static method takes one “this” parameter of the preceding class type.
The following code snippet creates a static class, defines a new static method and passes the AuthorElite class as a parameter.
- public static class ExtendedAuthorElite
- {
- public static ArrayList NewAuthors(this AuthorElite elite)
- {
- return new ArrayList() { "Author One", "Author Two", "Author Three" };
- }
- }
- static void Main(string[] args)
- {
- AuthorElite ae = new AuthorElite();
- ae.AllEliteAuthors();
- ae.NewAuthors();
- }
The following code snippet is the complete example of the previously explained example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections;
- namespace ExtensionMethodsSample
- {
- class Program
- {
- static void Main(string[] args)
- {
- AuthorElite ae = new AuthorElite();
- ae.AllEliteAuthors();
- ae.NewAuthors();
- }
- }
- public class AuthorElite
- {
- public ArrayList AllEliteAuthors()
- {
- return new ArrayList()
- {"Mahesh Chand", "Praveen Kumar", "Raj Kumar"};
- }
- }
- // Extended class
- public static class ExtendedAuthorElite
- {
- public static ArrayList NewAuthors(this AuthorElite elite)
- {
- return new ArrayList() { "Author One", "Author Two", "Author Three" };
- }
- }
- }
Summary
Extension methods were added to the C# language to provide C# programmers more extensibility on third-party types and class libraries. Here are some more articles on Extension Methods.
- How to Use Extension Methods in C# This article explains use of Extension Methods in C# to increase the functionality of a type (class).
- Extension Method In C# Extension Methods are a new feature in C# 3.0. An Extension Method enables us to add methods to existing types without creating a new derived type, recompiling, or modifying the original types.
- Overview of Extension Methods in C# This article is an overview of Extension Methods in C#.
- C# Extension Methods: Explained The intention of the article is to provide an idea of what extension methods are and their benefits.
- Practical Usage of Complete Extension Methods in LINQ In this article we will see the complete usage of extension methods in LINQ using a Lambda expression.
- Frequently Used Extension Methods in LINQ In this article we will see the frequently used extension methods in LINQ using Lambda expressions.
- Extension Methods in .NET To put it in a simple manner, Extension Methods have been introduced in the .NET 3.5 framework to add methods to a class without altering the code of that class.
- Extension Methods in C# .NET framework comes with a set of builtin classes and types that we use extensively in our applications. Integer, string, float and so on are some of these that are very frequently used.
- Extension Methods in .NET In this article we will see what extension methods are, why we need them and how to create them.

Join the conversation! Your thoughts help the community grow.