New feature in C# 11 and 12


C# 11 and 12 are both recent versions of the C# programming language, each introducing new features and enhancements. Here's a breakdown of the key differences between them:

C# 11 (released November 2022):

  • Focus: Improved productivity and expressiveness
  • Key features:
    • Raw string literals: Easier to write multiline strings without escaping characters.
    • Generic math support: Perform mathematical operations on generic numeric types.
    • Generic attributes: Apply attributes to generic types and methods.
    • UTF-8 string literals: Declare strings directly in UTF-8 encoding.
    • Newlines in string interpolation expressions: Improves readability of formatted strings.
    • List patterns: More powerful pattern matching for collections.
    • File-local types: Define types within a single file for better organization.
    • Required members: Ensure structs have specific members defined.
    • Auto-default structs: Simplify initialization of struct fields.
    • Pattern match Span on a constant string: More efficient string pattern matching.
    • Extended nameof scope: Use nameof with more expressions.
    • Numeric IntPtr: Represents IntPtr values as numbers.
    • ref fields and scoped ref: Improved support for references to fields.
    • Improved method group conversion to delegate: Reuses delegate objects for better performance.

C# 12 (released November 2023):

  • Focus: Performance and code maintainability
  • Key features:
    • Global usings: Reduce boilerplate code for commonly used namespaces.
    • Static local functions: Define functions within method bodies for better encapsulation.
    • Parameterless delegates: Simplify delegate creation for methods without parameters.
    • Improved performance for records: Faster initialization and member access for record structs.
    • Enhanced pattern matching: More flexible disjunction patterns and deconstruction with discard.
    • Inline arrays: Declare and initialize arrays directly in expressions.
    • Experimental attribute: Flag features under development for warnings.
    • Improved compiler diagnostics: More informative error messages and warnings.

Additional differences:

  • Supported .NET versions: C# 11 requires .NET 7 or later, while C# 12 requires .NET 8 or later.
  • Availability: C# 11 is currently more widely adopted, as C# 12 is newer.

Overall, both C# 11 and 12 offer valuable improvements for developers. The choice between which version to use depends on your specific needs and the .NET framework you're working with. If you're looking for features that enhance productivity and code readability, C# 11 is a good option. If you prioritize performance and maintainability, C# 12 might be more suitable.

New feature in C# 11 and 12 New feature in C# 11 and 12 Reviewed by Bhaumik Patel on 3:07 AM Rating: 5