1What Are Extension Methods?
Hi friends, in this article we will learn about extension methods in C#. They allow us to add new methods to an existing class without modifying its original source code or creating a derived class. A derived class is a class that inherits from another class. Extension methods make existing types easier to work with. You will commonly see them in LINQ, string helpers and reusable utility code.
2A Useful String Helper
Imagine you are building a shopping application. Different screens need to check whether a product name contains useful text. Instead of repeating the same logic everywhere, you can create a helper method.
With an extension method, you can call that helper directly on a string, almost like it was already part of the string class.
3Rules Behind Extension Methods
1. Use a static class
An extension method must be declared inside a non-nested static class.
2. Add the this keyword
The first parameter uses this followed by the type you want to extend. This identifies the type that receives the method.
3. Call it like an instance method
An instance method is called on an object. Extension methods use the same calling style, even though they are static methods.
4. Namespace matters
The namespace containing the extension class must be in scope, usually through a using directive.
| Normal static call | Extension call |
|---|---|
| TextHelper.HasText(name) | name.HasText() |
4Create Your Own Extension Method
Here we create HasText() for the string type. It checks whether a string contains a non-whitespace character.
using System;
static class StringExtensions
{
public static bool HasText(this string value)
{
return !string.IsNullOrWhiteSpace(value);
}
}
class Program
{
static void Main()
{
string product = "Laptop";
string empty = " ";
Console.WriteLine(product.HasText());
Console.WriteLine(empty.HasText());
}
}
5See the Program Result
Laptop contains text, so the first call returns true. The second string contains only spaces, so it returns false.
True False
6Watch the Method in Action
Follow the extension class, its this parameter and the calls made on the string variables.
7Key Points to Remember
Extension methods help keep reusable logic close to the type it works with. They improve readability without changing the original class.
- Declare them inside a static class.
- Use this on the first parameter.
- Make the extension namespace available to call the method.
8Interview Questions to Practise
What is an extension method in C#?
Answer: It is a static method that can be called using instance-method syntax on an existing type, without modifying that type.
Where must an extension method be declared?
Answer: Inside a non-nested static class.
Why do we use this in the first parameter?
Answer: It identifies the type being extended and makes the method available through that type's instance syntax.
Can extension methods access private members?
Answer: No. They follow normal access rules and cannot access private members of the extended type.
What happens if an instance method has the same name?
Answer: The instance method takes precedence over an extension method during method resolution.