/errors/csharp/cs0246-type-or-namespace-not-found
C# error CS0246: The type or namespace name could not be found
- 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
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
dotnet add package Newtonsoft.Json
dotnet buildWhen 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:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>Checklist
| Where the type lives | Fix |
|---|---|
| NuGet package | dotnet add package NAME |
| Another project in the solution | dotnet add reference ../Lib/Lib.csproj |
| A namespace of an already referenced assembly | using Namespace; at the top of the file, or a global using |
| The base class library | Check the TargetFramework; some APIs need a newer .NET |