Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported [duplicate]

I have been having this issues while testing the new features of C# 9.0 with Visual Studio 2019 Preview. I was testing the init setter, but the compiler shows error with the message:

Error CS0518 Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported. Below is the code snippet that I've tried:

public class Book
{
     string ISBN { get; init; }
}

This is a small bug in Visual Studio 2019 that hasn't been fixed yet. To solve this, you need to add a dummy class named IsExternalInit with the namespace System.Runtime.CompilerServices anywhere in your project. That will do it.

If writing a library it's best to make this class internal, as otherwise you can end up with two libraries both defining the same type.

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

Edit (November 16, 2020):

According to a reply I got from the Principle Developer Lead on C# Language Team, Jared Parsons, the issue above is not a bug. The compiler throws this error because we're compiling a .NET 5 code against older .NET Framework version. See his message below:

Thanks for taking the time to file this feedback issue. Unfortunately this is not a bug. The IsExternalInit type is only included in the net5.0 (and future) target frameworks. When compiling against older target frameworks you will need to manually define this type.

Link to the report on Visual Studio Developer Community: https://developercommunity.visualstudio.com/content/problem/1244809/error-cs0518-predefined-type-systemruntimecompiler.html


If you want to stay with .NET Core App 3.1, you will need to add the type like Kinin Roza explained in in this bug report.

However, if you change your csproj to have the <TargetFramework> set to net5.0, it will solve your problem as this type is only defined in 5.0.

Here's my sample Console app csproj file.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>