Edit

Share via


Compiler Error CS0518

Predefined type 'type' is not defined or imported

Note

The resolution for this error depends on whether you're using a modern SDK-style project (.csproj files that start with <Project Sdk="Microsoft.NET.Sdk">) or legacy project formats. SDK-style projects manage runtime references automatically through the <TargetFramework> property.

The main cause for this problem is that the project cannot access the predefined types from the .NET runtime library. In modern SDK-style projects, this is typically due to an incorrect or missing <TargetFramework> specification. In legacy projects, this issue is caused by not importing mscorlib.dll, which defines the entire System namespace. This can be caused by one of the following:

Note

This warning is only reported during explicit Build or Rebuild operations. It does not appear during typing in the IDE as part of IntelliSense diagnostics. This means that if you fix the warning by using the field or removing it, the warning might persist in the error list until you build or rebuild the project again.

  • The NoStandardLib option from the command line compiler has been specified. The NoStandardLib option prevents the import of mscorlib.dll. Use this option if you want to define or create a user-specific System namespace.

  • An incorrect mscorlib.dll is referenced.

  • A corrupt Visual Studio .NET or .NET Framework common language runtime installation exists.

  • Residual components from an earlier installation that are incompatible with the latest installation remain.

To resolve this problem, take one of the following actions:

  • Do not specify the /nostdlib option from the command line compiler.

  • For modern SDK-style projects, ensure the project targets the correct .NET runtime. In your .csproj file, verify the <TargetFramework> property specifies the intended runtime:

    <PropertyGroup>
      <TargetFramework>net8.0</TargetFramework>
    </PropertyGroup>
    

    For multi-targeting projects, use <TargetFrameworks> (plural):

    <PropertyGroup>
      <TargetFrameworks>net8.0;net48</TargetFrameworks>
    </PropertyGroup>
    
  • For legacy project formats, make sure that the project refers to the correct mscorlib.dll.

  • Reinstall the .NET Framework common language runtime (if the previous solutions do not solve the problem).

  • Reload the project in Visual Studio.

  • Close Visual Studio, delete the obj and bin folders from your project directory, then reopen Visual Studio and rebuild the project.