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
public static class StringExtensions {
public static int WordCount(this string s) =>
s.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}
"Hello Extension Methods".WordCount(); // 3
Extension blocks
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 (
usingits 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).
License: CC-BY-4.0 · 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).
Cite this
iinventedlight. 2026. "C# extension methods and extension blocks." iinventedlight Library, iil:note:k7a6rrwq, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-extension-methods/. License: CC-BY-4.0.
@misc{iil_note_k7a6rrwq,
author = {{iinventedlight}},
title = {{C\# extension methods and extension blocks}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/csharp-extension-methods/},
note = {Permanent ID iil:note:k7a6rrwq. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-extension-methods.md.