Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Extension Method this Parameter Explained

Understand the `this` parameter in C# extension methods: syntax, method resolution, generic extension methods, and common pitfalls.

~3 min readC#
#Extension Methods#C# Programming#Static Classes#LINQ
Thumbnail representing C# extension method syntax with a this parameter binding to a type.

In C#, the this keyword in an extension method marks the first parameter of a static method so the compiler treats the method as an extension of that type. The parameter is not a normal argument, and it is what lets you call the method as if it were an instance member even though it is defined in a static class. This syntax underpins LINQ and many library designs, so it helps to know exactly how it works and what it does not do.

How the this Parameter Changes Method Resolution

An extension method is declared inside a static, non-generic class. The first parameter gets the this keyword:

csharp
public static class StringExtensions { public static bool IsNullOrWhitespace(this string value) { return string.IsNullOrWhiteSpace(value); } }

The compiler translates a call like this:

csharp
string text = " "; bool empty = text.IsNullOrWhitespace();

into a static call:

csharp
bool empty = StringExtensions.IsNullOrWhitespace(text);

The extension method is syntactic sugar. At runtime there is no new method on string; the static method receives the instance as its first argument. This means extension methods cannot access private members of the type they extend, and they behave like static methods when called explicitly.

Requirements for the this Parameter

Several rules govern where and how the this parameter can appear. Violating them produces compile errors, so it is useful to know the boundaries.

  • The containing class must be static and non-generic.
  • The method itself must be static.
  • The this keyword can only appear on the first parameter, and the first parameter cannot have ref or out.
  • The first parameter can be a nullable value type, a reference type, an interface, a base class, or a type parameter introduced by the generic method. The containing class cannot introduce type parameters because it must be non-generic; the method itself can be generic.

Here is an example using a nullable value type:

csharp
public static class IntExtensions { public static bool IsOdd(this int? value) { return value.HasValue && value.Value % 2 != 0; } }

The this int? parameter means the method can be called on a nullable int. When the variable is null, the method still receives a null argument, and the nullable check inside the method handles it correctly.

Accessing the Instance Inside the Method

Inside the extension method, the first parameter is just a normal parameter. You refer to it by its name, not by the this keyword. Some developers new to extension methods expect this to be available inside the method, but that is incorrect.

csharp
public static string UppercaseFirstLetter(this string input) { if (string.IsNullOrEmpty(input)) return input; char first = char.ToUpperInvariant(input[0]); return first + input.Substring(1); }

The parameter input holds the instance that was used in the calling expression. If you call name.UppercaseFirstLetter(), the value of name is passed as input. You cannot use this inside the method because the method is static and has no instance context.

Why this Matters for Generic Extension Methods

Extension methods often target generic types. The this parameter can be a generic type, which lets you extend a family of types without knowing the exact type at compile time.

csharp
public static class EnumerableExtensions { public static bool IsEmpty<T>(this IEnumerable<T> source) { return !source.Any(); } }

Here the this parameter is IEnumerable<T>. When you call myList.IsEmpty(), the compiler infers T from the type of myList. This is similar to how LINQ methods work. The this parameter type can also be a specific interface, base class, or constructed generic type like List<int>. This example relies on LINQ's Any method, so it requires using System.Linq in scope.

Common Mistakes with the this Parameter

One frequent problem is forgetting that the containing class must be static. If you remove static from the class declaration, the method is no longer an extension method and the compiler rejects it:

csharp
public class StringExtensions { public static bool IsNullOrWhitespace(this string value) { return string.IsNullOrWhiteSpace(value); } }

Another common mistake is placing this on a parameter other than the first. The compiler accepts this only on the first parameter, so a declaration like (string value, this int count) is invalid.

Finally, extension methods do not override or replace instance methods. When a type has an applicable instance method with the same signature, C# prefers that instance method and does not call the extension method. That behavior is by design and is one reason extension methods are best used when you do not control the type you are extending.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.