Skip to content

/errors/csharp/cs0246-type-or-namespace-not-found

C# error CS0246: The type or namespace name could not be found

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
tool
C#
exit code
1
versions
.NET 6 – 10
os
any

error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?) means the C# compiler cannot find a definition for X. Either the assembly that defines it is not referenced (missing NuGet package or project reference), or its namespace is not imported with using. Add the package or reference, then the using directive.

Reproduce it

bash
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o app >/dev/null 2>&1
cd app
cat > Program.cs <<'EOF'
using Newtonsoft.Json;
Console.WriteLine(JsonConvert.SerializeObject(new { ok = true }));
EOF
dotnet build -nologo 2>&1 | grep -o 'error CS[0-9]*: [^[]*' | sort -u
exit ${PIPESTATUS[0]}

Output · exit 1

error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

The using line fails because no referenced assembly contains a Newtonsoft namespace. The message names the first part of the name that cannot be resolved, here the namespace rather than JsonConvert.

Fix: reference the package

bash
dotnet add package Newtonsoft.Json
dotnet build

When the type exists but only the namespace import is missing, the error is the same and the fix is the using line. The project file then contains:

app.csproj
<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

Checklist

Where the type livesFix
NuGet packagedotnet add package NAME
Another project in the solutiondotnet add reference ../Lib/Lib.csproj
A namespace of an already referenced assemblyusing Namespace; at the top of the file, or a global using
The base class libraryCheck the TargetFramework; some APIs need a newer .NET

Sources