Rok
Rok

Reputation: 741

Is is possible to use library with C# 9 Records in .NET Framework 4.x

Is there a way to create library with records and then use it in legacy .NET Framework 4.x application?

Upvotes: 10

Views: 2730

Answers (1)

Yair Halberstadt
Yair Halberstadt

Reputation: 6821

Yes, although this is not officially supported.

Firstly you have to set the LangVersion to 9, which you can do by manually editing the csproj, e.g.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net472</TargetFramework>
        <LangVersion>9.0</LangVersion>
    </PropertyGroup>

</Project>

You then have to add the following file to your library:

namespace System.Runtime.CompilerServices { internal class IsExternalInit { } }

After that you should be able to declare and use records as normal in your library.

Upvotes: 14

Related Questions