LINQPad
extension methods
C#
programming
software tools

LINQPad extension methods

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Extension methods are especially useful in LINQPad because they let you add small reusable helpers to queries without building a full project first. The mechanics are the same as in normal C#: define a static method in a static class and mark the first parameter with this. The LINQPad-specific question is mostly about where to place those methods so your queries can see them.

Define an Extension Method in the Query

For one-off exploration, you can define the extension method directly in the same LINQPad query.

csharp
1void Main()
2{
3    var numbers = new[] { 1, 2, 3, 4, 5 };
4    numbers.EveryOther().Dump();
5}
6
7public static class EnumerableExtensions
8{
9    public static IEnumerable<T> EveryOther<T>(this IEnumerable<T> source)
10    {
11        var take = true;
12        foreach (var item in source)
13        {
14            if (take)
15            {
16                yield return item;
17            }
18            take = !take;
19        }
20    }
21}

LINQPad compiles the whole query, so extension methods declared in the same script are immediately available for testing and iteration.

Make Reusable Extensions Available Everywhere

If you want the method in many queries, move it into LINQPad's shared extensions area instead of redefining it every time. A common workflow is to place reusable helpers in My Extensions, then reference them from later queries.

The main benefit is not syntax. It is reuse. Once the extension lives in your shared helper area, every future query can call it without copying boilerplate around.

Extension Methods Are Great for Query-Time Helpers

LINQPad is often used for quick inspection and transformation of data, so small helpers pay off quickly. For example, a string helper that normalizes whitespace:

csharp
1void Main()
2{
3    "  alpha   beta   gamma  ".NormalizeSpaces().Dump();
4}
5
6public static class StringExtensions
7{
8    public static string NormalizeSpaces(this string value)
9    {
10        return string.Join(
11            " ",
12            value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
13        );
14    }
15}

That kind of method is ideal in LINQPad because it turns repetitive exploration logic into a readable verb.

Use Extensions to Keep Queries Readable

Without extension methods, query scripts often grow a pile of local helper calls that break the visual flow of the data transformation. Extensions keep the code close to how you think about the operation.

csharp
1void Main()
2{
3    var users = new[]
4    {
5        new User("Alice", true),
6        new User("Bob", false),
7        new User("Cara", true)
8    };
9
10    users.ActiveNames().Dump();
11}
12
13public record User(string Name, bool IsActive);
14
15public static class UserExtensions
16{
17    public static IEnumerable<string> ActiveNames(this IEnumerable<User> users)
18    {
19        return users.Where(u => u.IsActive).Select(u => u.Name);
20    }
21}

This reads naturally inside the query and keeps the data shaping logic reusable.

Know the Usual Rules Still Apply

LINQPad does not change the C# language rules for extension methods. The method still must live in a static class, still must be static itself, and still needs this on the first parameter.

If you forget one of those rules, LINQPad will not treat the method as an extension:

csharp
1public static class BadExample
2{
3    public static IEnumerable<int> Squares(IEnumerable<int> source)
4    {
5        return source.Select(x => x * x);
6    }
7}

That method is callable, but not as numbers.Squares() because the first parameter is missing this.

Common Pitfalls

The biggest mistake is defining the method correctly but forgetting the static class requirement. In that case the code compiles only after changes, but the method is not usable the way you expect.

Another issue is putting too much application logic into LINQPad-only helpers. Extension methods are great for exploration, but if they become important production behavior, they should live in a normal shared library rather than only in a LINQPad environment.

People also sometimes overuse extension methods for operations that hide too much work. The best LINQPad helpers are small, obvious, and query-friendly.

Finally, if you move helpers into shared extensions, remember that changing them affects all future queries that rely on them. That is convenient, but it also means those helpers should stay stable and easy to understand.

Summary

  • LINQPad extension methods are ordinary C# extension methods used inside LINQPad queries.
  • For one-off work, define them in the query itself.
  • For reuse, move them into LINQPad's shared extension area.
  • Keep extension methods small and focused so queries stay readable.
  • If a helper becomes core application logic, consider promoting it out of LINQPad into a real shared codebase.

Course illustration
Course illustration

All Rights Reserved.