🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 8 Min Lesezeit
0

Extension Blocks in C# 14 Are More Than New Syntax

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Extension methods have been part of C# for years. Extension blocks finally make them feel like a small, organized API.



Extension methods are one of those C# features that quietly end up everywhere. LINQ depends on them, library authors use them to improve APIs they do not control, and most production codebases eventually collect a few SomethingExtensions classes.



The familiar syntax works, but it starts to feel awkward when several related operations target the same type:




CODE
public static class DateOnlyExtensions
{
public static bool IsWeekend(this DateOnly date) =>
date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday;

public static DateOnly StartOfMonth(this DateOnly date) =>
new(date.Year, date.Month, 1);

public static DateOnly NextBusinessDay(this DateOnly date)
{
var candidate = date.AddDays(1);

while (candidate.IsWeekend())
{
candidate = candidate.AddDays(1);
}

return candidate;
}
}






There is nothing technically wrong with this code. The repetition is small, and the calling side is already clean:




CODE
var releaseDate = new DateOnly(2026, 7, 24);

Console.WriteLine(releaseDate.IsWeekend());
Console.WriteLine(releaseDate.StartOfMonth());
Console.WriteLine(releaseDate.NextBusinessDay());






Still, the implementation reads like a static utility class rather than a coherent API for DateOnly.



C# 14 gives us another option.






The extension block syntax



C# 14 introduces extension blocks, which group members around a receiver inside a non-nested, non-generic static class. Unlike the older syntax, an extension block can contain properties as well as methods. It can also define members that appear to belong to the extended type itself rather than one particular instance. C# 14 ships with .NET 10. documents these lookup rules and confirms that extension-block methods and classic extension methods compile to the same IL.



This matters when naming extensions for types you do not own. A future framework or package update could add a member with the same name and change which implementation a call resolves to.



Extension members also cannot reach into private state. They only have access to members that would otherwise be accessible from the extension class. The new syntax improves organization, but it does not bypass encapsulation.






Should existing extension methods be rewritten?



Probably not just for the sake of using new syntax.



This existing method remains perfectly reasonable:




CODE
public static string Truncate(
this string value,
int maximumLength)
{
ArgumentOutOfRangeException.ThrowIfNegative(maximumLength);

return value.Length <= maximumLength
? value
: value[..maximumLength];
}






The extension-block equivalent is not automatically better:




CODE
public static class StringExtensions
{
extension(string value)
{
public string Truncate(int maximumLength)
{
ArgumentOutOfRangeException.ThrowIfNegative(
maximumLength);

return value.Length <= maximumLength
? value
: value[..maximumLength];
}
}
}






The new form becomes more convincing when the same receiver has multiple related members, when a property communicates intent better than a method, or when a static extension member genuinely improves discoverability.



For a mature codebase, I would migrate only when a file is already being changed and the new form makes the API easier to understand. A repository-wide conversion would create a large diff without necessarily changing the experience for callers.



Microsoft’s own guidance also recommends modifying the original type when you control it and the functionality genuinely belongs there. Extensions are most useful when the source is outside your control, inheritance is unsuitable or the behavior belongs to a particular application layer.






A practical review checklist



Before adding an extension block, I would ask a few questions:




  • Does this functionality naturally belong beside the extended type?

  • Is the original source unavailable or inappropriate to modify?

  • Does a property perform cheap, unsurprising work?

  • Are related extensions grouped in a focused namespace?

  • Could the chosen name collide with a future member?

  • Would a wrapper, service or ordinary method make the dependency clearer?



If the answers are reasonable, extension blocks can produce an API that feels much more deliberate than a growing collection of utility methods.






Final thoughts



C# 14 extension blocks are easy to dismiss as rearranged extension-method syntax. For a single method, that assessment is fair. The older syntax is familiar, compact and still fully supported.



The feature starts to earn its place when a type needs a small family of related operations. Properties can express state-like questions, methods can represent actual work, and static extensions can expose narrowly scoped factory-style behavior. All of them can live together around one receiver.



That makes the implementation easier to scan and the calling code closer to the API we meant to design in the first place.






Mini-challenge



Create an extension block for HttpResponseMessage with:




  • An IsRetryable property for status codes your application considers temporary.

  • A HasJsonContent property that checks the response content type.

  • A ReadRequiredJsonAsync<T>() method that throws a useful exception when the response cannot be deserialized.



The interesting part is not getting the code to compile. It is deciding which operations deserve property syntax and which should remain methods.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Extension Blocks in C# 14 Are More Than New Syntax

Thematisch verwandte Begriffe: Extension, Blocks, More, Than · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...