The Future is Delightful: A Peek into What’s Next for C# 13
C# Lead Designer, Mads Torgersen, recently gave a talk in Sydney, offering a glimpse into the exciting possibilities for the future of the language. While he couldn’t reveal everything in the works for C# 13 and beyond, he shared key insights into the design philosophy and upcoming features that aim to make coding in C# even more enjoyable and efficient.
Continuing the Legacy of Delightful Coding
Torgersen emphasized the C# team’s core principle: the language should be a joy to use. They strive to eliminate frustrations and “gotchas” that can make coding tedious, ensuring that C# remains a compelling choice for developers’ next projects.
Building on the Success of Collection Expressions
Collection Expressions were a standout feature in C# 12, unifying the syntax for creating different collection types. Future enhancements aim to further expand their capabilities, including:
- Natural Type Inference: The team is exploring how to assign a “natural” type to collection expressions, potentially allowing for seamless integration with features like foreach loops and LINQ queries.
- Parameter Spans: Imagine passing collections directly as method parameters without explicitly creating an array! This feature, leveraging the efficient span type, is on the horizon for C# 13.
- Dictionary Support: Creating dictionaries with a concise and consistent syntax is another area being actively explored.
Breaking Things for a Better Tomorrow
While backward compatibility is crucial, Torgersen acknowledged that sometimes small, strategic breaking changes are necessary for long-term language health. The team is carefully considering a more permissive approach to breaking changes, focusing on situations where:
- User benefits are clear: The change should offer a significantly improved design or functionality.
- Breaks are localized and diagnosable: The impact should be easily identified and fixed, with automated tools assisting in the upgrade process.
- Default fixes are available: An obvious, non-disruptive automated solution should be provided to address the breaking change.
A new feature allowing developers to access auto-property backing fields directly serves as a test case for this new strategy.
Unifying Concepts: Explicit and Implicit Extensions
Torgersen delved into the concept of “extensions,” providing a powerful mechanism for extending existing types without modifying their original code.
- Explicit Extensions: These act as named aliases for existing types, enhancing code readability, and allowing developers to add context-specific functionality. Imagine defining a “JsonString” extension for strings containing JSON data, complete with methods for parsing and manipulation.
- Implicit Extensions: Taking the concept further, implicit extensions automatically apply their added functionality to the underlying type within a specific scope. This enables developers to effectively add methods and properties to existing types, offering a powerful approach to code organization and extensibility.
Exploring the Potential of Union Types
While not slated for C# 13, Union Types are a hot topic in the C# world, sparking discussions about their potential benefits and challenges. Two main camps exist:
- Discriminated Unions (Functional Programming Style): Often used as the primary data structuring mechanism in functional languages, these unions explicitly tag different data shapes within a single type.
- Type Unions (TypeScript Style): These offer a more ad-hoc approach, allowing developers to specify that a variable can hold one of several types without requiring explicit tagging.
Torgersen highlighted the potential for a unified approach, where “cases” within a union are treated as types themselves, bridging the gap between discriminated and type unions. This could offer a powerful and flexible way to model data in C#, further enhancing the language’s expressiveness and elegance.
What’s next in C# — Mads Torgersen — NDC Sydney 2024 — YouTube
Looking Ahead: A Bright Future for C#
While these are just a few highlights, Torgersen’s talk painted an exciting picture of the future of C#. The team is committed to staying at the forefront of language design, incorporating innovative features while prioritizing developer experience and code clarity. With a focus on “delightful coding,” the future of C# promises to be both powerful and enjoyable.