Testing C# 9.0 in VS2019 - CS0518 IsExternalInit is not defined or imported ... How do I define/import it?

Solution 1:

This is a bug in the current preview and the latest master branch (June 27). A simple record in sharplab.io creates the same error.

Just add the missing type somewhere in your project

using System.ComponentModel;

namespace System.Runtime.CompilerServices
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    internal class IsExternalInit{}
}

Records and init will work without problem.

Only LinqPad 6 seems to work without problems, probably because it includes that type too

Solution 2:

Borrowing from Panagiotis Kanavos, you can actually declare IsExternalInit as a record itself (self fulfilling prophecy):

using System.ComponentModel;

namespace System.Runtime.CompilerServices
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public record IsExternalInit;
}

Solution 3:

If you don't like to litter each and every project with the IsExternalInit class, you can use Directory.Build.props to automatically reference the file as an invisible compile item:

<?xml version="1.0" encoding="utf-8"?>
<Project>
    <ItemGroup>
        <Compile Include="$(MSBuildThisFileDirectory)IsExternalInit.cs" Visible="false" />
    </ItemGroup>
</Project>

Add/edit this file in your solution's root and add the IsExternalInit.cs file there as well. Doing so will add that file to all your projects automatically.

Example folder structure relative to your solution:

.
├── Directory.Build.props
├── IsExternalInit.cs
└── Solution.sln