Extension methods allow developers to add new methods to existing types (classes, interfaces, structs) without modifying the original source code or creating a new derived type.
A quick metaphor
Imagine you've bought a new basic bicycle. It's nice, but wouldn't it be cool if you could add a basket without welding or making permanent changes? That's what Extension Methods in C# are like!
So, what are Extension Methods?
They let you add new methods (or features) to existing classes (like our bicycle) without changing their original code.
The Creation process
Make a static method (a method that belongs to a class, not an object) inside a static class (a class that can't be instantiated).
The first parameter of this method uses the this keyword and mentions the type (or class) you want to add a method to.
The Rules
It must be defined in a static class.
It must be a static method.
The first parameter specifies the type you're extending, prefixed with the this keyword.
Quick example:
For instance, in C#, strings don't have a built-in method to count words. But with Extension Methods, you can add one:
public static class StringHelpers
{
public static int CountWords(this string phrase)
{
return phrase.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Output:
string myText = "C# is fun!";
int totalWords = myText.CountWords(); // This gives 3.
Why Use Extension Methods?
Readability: They can make the code more intuitive and readable.
Reusable Code: Common utility functions can be turned into extension methods for reuse across projects.
Extending Sealed Classes: They allow adding methods to sealed classes or types from third-party libraries.
Important Points to Consider
No Access to Private Members: Extension methods can't access private members of the type they're extending.
Discoverability: New team members might find it challenging to discover extension methods compared to traditional methods.
Overuse: Avoid turning every utility function into an extension method. Use them when it genuinely enhances code readability or utility.
Real-world Applications
LINQ: Language Integrated Query (LINQ) extensively uses extension methods. For instance, methods like Where or Select are extension methods provided for collections.
Serialization/Deserialization: Convert objects to JSON or XML and vice-versa without cluttering the original class.
The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods
Comments