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

2

96

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; }
}
Woald answered 9/11, 2020 at 9:57 Comment(0)
H
170

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

Hortatory answered 9/11, 2020 at 9:58 Comment(8)
Is there a link to the bug report so we can known when it gets fixed?Hypothalamus
@DavidLechner please see this link. Developer at Microsoft replied to my reporting. developercommunity.visualstudio.com/content/problem/1244809/…Hortatory
I still had my project configured for netstandard (even though i switched to version 9). Was a silly mistake, but this answer certainly helped point that out.Mcgowan
Just thinking out loud - if it's not a bug, then why it is not in the documentation in the first place and why am I reading it on the StackOverflow :)Background
I'm getting this error even in a net5.0 project.... at random. Some days it builds, some days it doesn't... losing my mind.Resoluble
I don't get it. Why are there language version and runtime version if a language feature is only available on a certain runtime? Isn't the C# language specification itself independent of the .NET runtime?Fiske
I also recieve this in Rider IDE by Jetbrains, so I don't know if it's an issue with vs.Kattie
Imho, it's better to wrap the code with #if !NET5_0_OR_GREATER #endifOaks
L
8

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>

Liege answered 13/11, 2020 at 15:15 Comment(2)
I had this issue event with .Net5.0 , not sure whyBoiardo
The solution works with .Net 7.0. After adding <TargetFramework>net7.0</TargetFramework> the problem resolved automatically.Chlordane

© 2022 - 2024 — McMap. All rights reserved.