'C# Predefined type 'System.Object' is not defined or imported

In .NET core project, I changed the all projects name than after reloading all project that time many errors were showing approx 3225. And mainly all errors are related to system namespace like

  • System.Object not defined or imported
  • System.Boolean not defined or imported
  • Task does not exist
  • namespace ArgumentNullException could not be found

How do I resolve all those errors?



Solution 1:[1]

I was hitting this in VS Code. In Windows Terminal, dotnet build and dotnet run ran fine, but I was getting the errors in VS Code. The delete/restore/reopen answer did not fix it.

It seems it was an issue where I had an outdated version of Visual Studio confusing the C#/OmniSharp extension in VS Code.

The OmniSharp log included something like this:

[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 2 MSBuild instance(s)
            1: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
            2: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"

That version of Visual Studio 2019 did not have the .NET 6.0 SDK installed, and I couldn't find it in the VS Installer.

So I installed Visual Studio 2022 and the .NET 6.0 SDK as part of that install. Now my log looks like this:

[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 3 MSBuild instance(s)
            1: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"
            2: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
            3: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"

And the errors are gone!

Solution 2:[2]

This can also be caused by a circular dependency between projects in a solution.

error MSB4006: There is a circular dependency in the target dependency graph involving target "_GenerateRestoreProjectPathWalk"

Solution 3:[3]

Just like citelao I've got this issue in VS code when created new .NET 6.0 isolated AZ Function using wizard.
In OmniSharp logs I had about the same.

Just in case, in order to see the omni sharp logs, you have to open 'output' panel, and there on the right click on a drop-down: enter image description here

I did not want to install VS 2022 as my target was to work in VS code for this project. I found the answer here: omnisharp-roslyn #2247

Basically the solution was to add omnisharp.json file in the project root with the following contents:

{
  "msbuild": {
  "useBundledOnly": true
  }
}

After that I've closed VS Code, deleted bin and obj folders and started VS Code back.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 GeorgeP
Solution 3 Dmitry Mefed