Skip to content
Latchkey

C# "CS0246: type or namespace name could not be found" in CI

The compiler saw a type or namespace name it cannot resolve. CS0246 almost always means a missing reference (package or project) or a missing using - not a syntax problem. In CI it frequently comes from a package that restored locally but not on the runner.

What this error means

Build fails with CS0246 naming the unresolved type/namespace, often hinting "are you missing a using directive or an assembly reference?". Deterministic for a given source + reference set.

dotnet
Program.cs(7,9): error CS0246: The type or namespace name 'JsonSerializer' could not be
found (are you missing a using directive or an assembly reference?)

Common causes

A package or project reference is missing

The type lives in a NuGet package or sibling project that is not referenced, so the compiler cannot find it.

A using directive is missing

The type exists in a referenced assembly but the namespace is not imported, so the unqualified name does not resolve.

How to fix it

Add the missing reference

  1. Identify which package or project defines the type.
  2. Add the PackageReference or ProjectReference.
  3. Restore and rebuild.
.csproj
<ItemGroup>
  <PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

Add the using directive

  1. Add the namespace import for the type at the top of the file (or via global usings).
  2. Rebuild to confirm resolution.
.cs
using System.Text.Json;

How to prevent it

  • Ensure restore runs and succeeds before build in CI.
  • Use global usings for ubiquitous namespaces to reduce per-file gaps.
  • Commit a lock file so the same packages resolve on every runner.

Frequently asked questions

What causes ""CS0246: ... could not be found""?
The type lives in a NuGet package or sibling project that is not referenced, so the compiler cannot find it.
How do I fix "CS0246: ... could not be found"?
Add the missing reference

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card