---
source: https://iinventedlight.com/library/notes/csharp-extension-methods.md
title: "C# extension methods and extension blocks"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates cited source lines"
attribution: "Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Extension members - C#, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Extension member declarations - C# reference, CC BY 4.0 (dotnet/docs)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: C#"
tags: ['csharp', 'dotnet', 'extension-methods', 'linq']
sources:
  - id: S1
    title: "Extension members - C#"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "Extension member declarations - C# reference"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extension
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# C# extension methods and extension blocks

**Answer:** Extension members let you add methods to an existing type without subclassing, recompiling or modifying it. The classic form is a static method whose first parameter uses `this`; newer C# adds `extension` blocks. An extension block is a block in a non-nested, nongeneric, static class that contains extension members for a type or an instance of that type. (S1)

## Classic extension method

```csharp
public static class StringExtensions {
  public static int WordCount(this string s) =>
    s.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}

"Hello Extension Methods".WordCount(); // 3
```


## Extension blocks

```csharp
public static class NumericSequences {
  extension(IEnumerable<int> seq) {
    public IEnumerable<int> AddEach(int n) => seq.Select(x => x + n);
  }
}
```

Extension blocks can be generic, and individual members can add their own type parameters.

## Rules

- The extension must be in scope (`using` its namespace).
- An instance method on the type always wins over an extension with the same signature.
- Extensions cannot access private members.

## Claims
- id: C1
  claim: Extension members add methods to existing types without modifying them.
  source: S1
  evidence: "methods to existing types without creating a new derived type"
- id: C2
  claim: Extensions must be inside a non-nested, nongeneric static class.
  source: S1
  evidence: "Both forms of extensions must be defined inside a non-nested, nongeneric static class."
- id: C3
  claim: The docs show extension members for numeric sequences.
  source: S2
  evidence: "/// Contains extension members for numeric sequences."
- id: C4
  claim: Extension blocks can be generic.
  source: S2
  evidence: "Generic extension blocks"

## Sources
- [S1] Extension members - C#. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Extension member declarations - C# reference. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extension . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
