0

Similar to Does C# 8 support the .NET Framework?

I know that it's officially unsupported, but what features in C# 9 are available when using the .Net Framework (4.8)?

Features from -- https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-9

  • Records
  • Init only setters
  • Top-level statements
  • Pattern matching enhancements
  • Performance and interop
    • Native sized integers
    • Function pointers
    • Suppress emitting localsinit flag
  • Fit and finish features
    • Target-typed new expressions
    • static anonymous functions
    • Target-typed conditional expressions
    • Covariant return types
    • Extension GetEnumerator support for foreach loops
    • Lambda discard parameters
    • Attributes on local functions
  • Support for code generators
    • Module initializers
    • New features for partial methods
jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • 1
    It's unsupported, but apparently some language features work. https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb – David Browne - Microsoft Aug 18 '23 at 13:41

1 Answers1

6

To the best of my knowledge this is what is done by the compiler, and what requires runtime support (i.e. will not run on .net framework, requires .net core)

C# 9

  • Records (compiler only for .NET 4.0+, earlier versions would require additional work)
  • init only setters (compiler, but needs class IsExternalInit)
  • Top-level statements (compiler)
  • Pattern matching enhancements (compiler)
  • Native sized integers (eg nint and nuint, compiler)
  • Function pointers (runtime I believe, wasn't able to verify)
  • Suppress emitting localsinit flag (compiler, but needs class SkipLocalsInitAttribute)
  • Target-typed new expressions (compiler)
  • Static anonymous functions (compiler)
  • Target-Typed Conditional Expression (compiler)
  • Covariant return types (runtime)
  • Extension GetEnumerator support for foreach loops (compiler, was already in VB)
  • Lambda discard parameters (compiler)
  • Attributes on local functions (compiler, can be used for debug only static functions)
  • Support for code generators (compiler and ide)
  • Module initializers (compiler, needs class ModuleInitializerAttribute)
  • Extending Partial Methods (compiler)
jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • 1
    Caveat: Record types directly generate a call to `Type.op_Equality`, which was introduced in .NET 4.0, so when compiling against earlier versions, each record type has to provide your own implementation of `Equals` that doesn't use it. – madreflection Aug 18 '23 at 13:49
  • Extension GetEnumerator also has the additional requirement of the `ExtensionAttribute` class. I'm leaving *this* one as a comment (instead of editing the answer) because that's implied by being an extension method in .NET 2.0. – madreflection Aug 18 '23 at 14:10