Extension Methods in C# Simplified

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.

  1. public class AuthorElite  
  2. {  
  3. public ArrayList AllEliteAuthors()  
  4. {  
  5. return new ArrayList()  
  6. {  
  7.  "Mahesh Chand""Praveen Kumar""Raj Kumar"};  
  8. }  
  9. }  

 

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.

  1. public static class ExtendedAuthorElite  
  2. {  
  3. public static ArrayList NewAuthors(this AuthorElite elite)  
  4. {  
  5. return new ArrayList() { "Author One""Author Two""Author Three" };  
  6. }  
  7. }  
Now, let’s see how we can use the extended method. The following code snippet creates an instance of the AuthorElite type and as you can see, you have access to both the methods that were defined in the AuthorElite and the ExtendedAuthorElite classes.

 

  1. static void Main(string[] args)  
  2. {  
  3. AuthorElite ae = new AuthorElite();  
  4. ae.AllEliteAuthors();  
  5. ae.NewAuthors();  
  6. }  

 

The following code snippet is the complete example of the previously explained example.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Collections;  
  7.   
  8. namespace ExtensionMethodsSample  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. AuthorElite ae = new AuthorElite();  
  15. ae.AllEliteAuthors();  
  16. ae.NewAuthors();  
  17. }  
  18. }  
  19.   
  20. public class AuthorElite  
  21. {  
  22. public ArrayList AllEliteAuthors()  
  23. {  
  24. return new ArrayList()  
  25. {"Mahesh Chand""Praveen Kumar""Raj Kumar"};  
  26. }  
  27. }  
  28.   
  29. // Extended class  
  30.   
  31. public static class ExtendedAuthorElite  
  32. {  
  33. public static ArrayList NewAuthors(this AuthorElite elite)  
  34. {  
  35. return new ArrayList() { "Author One""Author Two""Author Three" };  
  36. }  
  37. }  
  38. }  

 

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.

 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.