How to Resolve Error "CS0518: Predefined type 'type' is not defined or imported" in C#
The Compiler Error CS0518 is a foundational library error. The message reads: "Predefined type 'System.Object' (or 'System.String', 'System.Int32') is not defined or imported".
In C#, keywords like int, string, bool, and object are not built into the compiler logic directly; they are aliases for types defined in the standard library (specifically System.Int32, System.String, etc.). If the compiler cannot find the DLL that defines these core types (typically mscorlib.dll or System.Runtime.dll), it cannot compile even the simplest code, resulting in CS0518.
This guide explains why these core definitions might go missing and how to restore them.
Understanding Predefined Types
When you write:
int x = 10;
The C# compiler actually treats this as:
System.Int32 x = 10;
For this to work, the compiler must have a reference to a library that contains a class named Int32 inside the System namespace. By default, Visual Studio and the .NET SDK automatically reference these standard libraries. CS0518 means this automatic link has been severed.
Scenario 1: The <NoStdLib> Configuration
This is the most common cause for this error in specialized projects. If you instruct the compiler not to load the standard library (usually because you are writing a custom kernel, an operating system, or a strictly controlled embedded application), you take on the responsibility of defining System.Object, System.Int32, etc., yourself.
Problematic Configuration
Check your .csproj file for the NoStdLib property.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<!-- ⛔️ Error: This tells the compiler "Don't load mscorlib.dll".
Consequently, 'int', 'string', and 'object' stop working immediately. -->
<NoStdLib>true</NoStdLib>
</PropertyGroup>
</Project>
Solution: Remove the Flag
Unless you are intentionally building a replacement for the .NET Runtime, simply remove this setting.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<!-- ✅ Correct: Allowing standard libraries fixes CS0518. -->
<!-- <NoStdLib>true</NoStdLib> -->
</PropertyGroup>
</Project>
Alternative Solution: Define the Types (Advanced)
If you must use NoStdLib, you must define the missing types yourself in the correct namespace.
// If NoStdLib is true, you must write this yourself:
namespace System
{
public class Object { }
public class Void { }
public struct Int32 { }
// ... and so on for every basic type used.
}
Scenario 2: Broken Framework References
In older .NET Framework projects (or migrated projects), references to mscorlib or System.Runtime might become corrupted or drop out of the reference list.
Example of error
- You see many CS0518 errors on basic types.
- The "References" or "Dependencies" node in Solution Explorer shows yellow warning triangles.
Solution: Clean and Restore
- Clear Caches: Close Visual Studio and delete the
binandobjfolders in your project directory. - Restore NuGet: Run
dotnet restoreor right-click the solution and select Restore NuGet Packages. - Check AssemblyInfo: Ensure you haven't manually added a conflicting
[assembly: ...]attribute that confuses the runtime loader.
Nuget Conflicts: Sometimes a package meant for an older framework (like Silverlight or Windows Phone) is installed into a modern .NET project, causing the compiler to lose track of the correct core library version. Check your installed packages.
Conclusion
CS0518 indicates that the C# language has lost its connection to the .NET Framework basics.
- Check
.csproj: Look for<NoStdLib>true</NoStdLib>. Remove it if found. - Check References: Ensure
System.Runtimeormscorlibis referenced. - Repair SDK: If a new project fails with this error immediately, your .NET SDK installation may be corrupted. Run the Visual Studio Installer and select Repair.