Extension Methods
What if you could add a method to string — a type you didn't write and can't change? Extension methods let you do exactly that. They're how LINQ bolts Where and Select onto every collection, and they're your tool for turning awkward helper calls into clean, readable, fluent code that reads like the method always belonged there.
Learn Extension Methods in our free C# course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.
Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of a phone case with a built-in bottle opener. You didn't redesign the phone — the manufacturer can't and won't add a bottle opener — but the case snaps on and gives your phone a new ability that feels built-in. Extension methods snap new abilities onto existing types the same way: the original type is untouched, yet at the call site it looks like the method was always there.
1. Writing Your First Extension Method
Three rules: the method must be in a static class, the method must be static , and the first parameter must have the this modifier naming the type you're extending. That's it — now "hello".Shout() works, even though string has no Shout method. It's pure syntactic sugar for a static call.
2. Extensions with Parameters
Anything after the this parameter is a normal argument. So age.IsBetween(18, 65) reads naturally while still being a plain static method underneath. You can extend any type — int , double , your own classes, even interfaces.
Your turn — write a Reversed() extension on string and call it.
3. Extensions on Collections (the LINQ pattern)
Extension methods on IEnumerable<T> are exactly how LINQ works. You can write your own and chain them right alongside Where and Select . Here's a custom MedianOrZero() that slots straight into a LINQ pipeline.
These lines define and use a Doubled() extension, but they're out of order. Arrange them so the program compiles and prints 14 .
The static class wrapping the static this -method must be declared before the call site can use 7.Doubled() .
36 — 6.Sq() calls the extension, returning 6 * 6.
abab — the extension concatenates the string with itself.
3. True or false: an extension method can override a real instance method of the same name.
False — instance methods always win; the extension is only used when no instance method matches.
Real teams keep call-sites readable by grouping little helpers as extensions. Here's a tiny string-validation toolkit you'd genuinely use.
Add a WordCount() method to string — the kind of small helper you'll reuse forever. Declare the static class, write the method, and call it.
Practice quiz
Where must an extension method be defined?
- In any class
- In an interface
- In a static, non-nested class
- In the type it extends
Answer: In a static, non-nested class. Extension methods must live in a static, non-generic, non-nested class (CS1109 otherwise).
What makes a static method an extension method?
- The 'this' modifier on the first parameter
Answer: The 'this' modifier on the first parameter. The 'this' modifier on the first parameter is the magic that turns an ordinary static method into an extension method.
The call on an extension is just sugar for what?
- value.GetMethod()
- new Method(value)
- Method.Invoke(value)
- Class.Method(value)
Answer: Class.Method(value). value.Method() is syntactic sugar for the plain static call Class.Method(value).
Do extension methods modify the original type's source code?
- Yes, they add real members
- No — they're resolved by the compiler at call sites
- Only for sealed types
- Only at runtime via reflection
Answer: No — they're resolved by the compiler at call sites. They don't change the type or add real members; the compiler resolves them at call sites, which is why you can extend types you don't own.
Can an extension method take extra parameters?
- Yes, anything after the 'this' parameter is a normal argument
- No, only the 'this' parameter is allowed
- Only one extra parameter
- Only optional parameters
Answer: Yes, anything after the 'this' parameter is a normal argument. Anything after the 'this' parameter is a normal argument, e.g. age.IsBetween(18, 65).
What must the method itself be (besides being in a static class)?
- public
- virtual
- static
- abstract
Answer: static. The method itself must also be static, not just the class (CS1106 otherwise).
If a type already has an instance method with the same name as an extension, which wins?
- The extension method
- The instance method
- It's a compile error
- Whichever is declared first
Answer: The instance method. Instance methods always win; the extension is only used when no matching instance method exists.
Why might an extension method not be found at the call site?
- The type is sealed
- The method isn't public
- The class is generic
- The namespace containing the static class isn't in scope (missing using)
Answer: The namespace containing the static class isn't in scope (missing using). To use an extension, the namespace of its static class must be in scope via a using directive — that's the usual 'method not found' cause.
How is LINQ (Where, Select, OrderBy) implemented?
- As keywords built into the compiler
- As extension methods on IEnumerable<T> in System.Linq
- As instance methods on List<T>
- As static methods on the Enumerable struct only
Answer: As extension methods on IEnumerable<T> in System.Linq. Where, Select, OrderBy and the rest are extension methods on IEnumerable<T> in System.Linq — that's why you add 'using System.Linq;'.
When should you NOT use an extension method?
- When extending string
- When the type is third-party
- When you own the type — add a real instance method instead
- When chaining with LINQ
Answer: When you own the type — add a real instance method instead. If you own the type, add a real method; extensions are for adding behaviour to types you can't change.