Extension Methods in C# (Interview Guide)
Extension methods are a powerful feature in C# that allow you to add new methods to existing types without modifying their source code.
This is very commonly asked in interviews.
What are Extension Methods?
Definition:
Extension methods allow you to extend existing classes, structs, or interfaces by adding new methods without inheritance or modifying original class.
They are heavily used in LINQ.
Why Use Extension Methods?
Extension methods help when:
✅ You don't have access to source code
✅ You don't want to create derived classes
✅ You want cleaner and readable code
✅ You want to add utility/helper methods
Basic Syntax
Extension methods must:
-
Be inside static class
-
Be static method
-
First parameter must use this keyword
public static class ExtensionClass
{
public static returnType MethodName(this Type obj)
{
// logic
}
}
Simple Example
Let's add a method to string
public static class StringExtensions
{
public static bool IsLong(this string value)
{
return value.Length > 10;
}
}
Now you can use it like this:
string name = "Hello World";
bool result = name.IsLong();
Console.WriteLine(result);
Output
True
This looks like IsLong() is part of string class — but it's actually an extension method.
Real World Example (Very Important for Interviews)
public static class NumberExtensions
{
public static bool IsEven(this int number)
{
return number % 2 == 0;
}
}
Usage:
int num = 10;
Console.WriteLine(num.IsEven());
Output:
True
Example from LINQ (Most Common Example)
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(x => x % 2 == 0);
Where() is actually an Extension Method.
How Extension Methods Work Internally
This:
num.IsEven();
Is converted internally to:
NumberExtensions.IsEven(num);
Rules for Extension Methods
✅ Must be static class
✅ Must be static method
✅ First parameter must use this
✅ Namespace must be included using using
Extension Method with Multiple Parameters
public static class MathExtensions
{
public static int Add(this int a, int b)
{
return a + b;
}
}
Usage:
int result = 5.Add(3);
Console.WriteLine(result);
Output:
8
Extension Methods vs Inheritance
| Extension Methods | Inheritance |
|---|---|
| No need to inherit | Requires inheritance |
| Cleaner syntax | More complex |
| Cannot override methods | Can override |
| Adds helper methods | Adds full functionality |
Interview Questions & Answers
1. What are Extension Methods?
Extension methods allow adding new methods to existing types without modifying them.
2. Where are Extension Methods used?
Mostly used in:
-
LINQ
-
Utility methods
-
String helpers
-
Collection helpers
3. Can Extension Methods access private members?
❌ No
They can only access public members
4. Can Extension Methods override existing methods?
❌ No
They only work if method doesn't already exist.
5. What is the advantage of Extension Methods?
-
Clean code
-
Reusability
-
No inheritance needed
-
Better readability
Advanced Example (Real Interview Level)
public static class DateTimeExtensions
{
public static bool IsWeekend(this DateTime date)
{
return date.DayOfWeek == DayOfWeek.Saturday
|| date.DayOfWeek == DayOfWeek.Sunday;
}
}
Usage:
DateTime today = DateTime.Now;
Console.WriteLine(today.IsWeekend());
When NOT to Use Extension Methods
Avoid when:
❌ You need to modify object state
❌ You need polymorphism
❌ You need method override
Best Practice
✔ Use Extension Methods for utility/helper methods
✔ Keep them small and readable
✔ Use meaningful naming
One-Line Interview Answer
Extension Methods allow adding new methods to existing types without modifying their source code using static methods and
thiskeyword.
