Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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

CS0246 is a compile-time resolution failure: a missing using, package, target framework, or an unrestored project. Trace it backward from the symbol.

~5 min readC#
#csharp#dotnet#cs0246#nuget
Bold white headline reading TYPE NOT FOUND with a .NET project crate holding an amber missing namespace card.

Introduction & Quick Summary

The compiler cannot resolve a symbol, so it stops on the first use:

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

CS0246 means the type name is unknown at compile time. It is not a runtime problem and not a NuGet download failure by itself; the compiler looked at every reference it was given and found no type with that name in a namespace it can see. The parenthetical in the message is the actual checklist: a missing using, or a missing assembly reference.

Where it appears:

  • A package was added to the project but not restored, or restore failed silently.
  • The type lives in a package that targets a different framework than the project.
  • The using statement names the wrong namespace or has a typo.
  • A project reference was removed during a refactor.
  • A file-scoped namespace or global using was assumed but ImplicitUsings is off.
  • A source generator or analyzer package was not referenced with the right attributes.

Quick answer: identify the package and namespace that own the type, add the reference, restore, and build. If both exist, check the target framework.

Core Mechanics, Signatures & Reproducible Setup

The C# compiler resolves a type through three inputs: the using directives in scope, the reference assemblies from the project file, and the target framework's reference set. PackageReference adds assemblies through NuGet restore; ProjectReference adds a sibling project's output; the shared framework adds System.* assemblies for the chosen framework.

xml
<!-- Broken.csproj: package missing --> <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> </Project>
csharp
// Broken.cs: ILogger lives in Microsoft.Extensions.Logging using Microsoft.Extensions.Logging; public class Worker { private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger) => _logger = logger; }
code
$ dotnet build Broken.cs(7,30): error CS0246: The type or namespace name 'ILogger<>' could not be found

The fix is one reference, and the build then resolves the type through the shared framework's extensions assemblies:

xml
<ItemGroup> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" /> </ItemGroup>

Neighbouring errors and what each tells you:

CodeMessageMeaning
CS0246type or namespace name could not be foundno type with that name is referenced
CS0234type or namespace does not exist in the namespacethe namespace exists, the member does not
CS0103name does not exist in the current contexta variable or member, not a type
CS1061does not contain a definition forthe type resolved, the member did not
CS0012type is defined in an assembly that is not referenceda dependency's dependency is missing
NETSDK1004assets file not foundrestore never ran

Key Comparison Table

Where the type livesReference to addWhere it goes
NuGet package<PackageReference Include="..." Version="..." />ItemGroup in the csproj
Another project in the solution<ProjectReference Include="..." />ItemGroup in the csproj
Framework assemblychange <TargetFramework>PropertyGroup
Generated by a generator<PackageReference> plus OutputItemType="Analyzer"ItemGroup
Local DLL<Reference Include="..."><HintPath>...</HintPath></Reference>ItemGroup, avoid when possible

Why the same symbol is missing in only one project:

SymptomCauseCheck
IDE shows no error, dotnet build failsrestore skipped or a stale obj folderdotnet restore, delete obj and bin
Fails in CI onlya NuGet.config with a private feed, or a locked mode restoredotnet restore --locked-mode output
Fails after a target framework changethe package has no assets for the new frameworkdotnet list package --include-transitive
Fails in one file onlya using inside a namespace blockthe file's namespace and usings
Fails with a valid packagepackage is marked private assetsthe package's PrivateAssets in the csproj

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Find the namespace instead of guessing it

The type name alone is not enough: JsonSerializer exists in System.Text.Json, and older code used Newtonsoft.Json. Search the package for the type, then add the exact using.

bash
dotnet package search Microsoft.Extensions.Logging --take 5 dotnet add package Microsoft.Extensions.Logging.Abstractions
csharp
using System.Text.Json; using System.Text.Json.Serialization; var json = JsonSerializer.Serialize(new { ok = true });

In Visual Studio, Ctrl+. on the red squiggle offers the using when the assembly is already referenced. If the suggestion is empty, the assembly is the missing part.

2. Fix a target framework mismatch

A package can compile for netstandard2.0 and still be unusable from a net8.0 project if the project uses an API that only exists in a newer asset. The reverse also happens: a net8.0-only package cannot be referenced from netstandard2.0.

xml
<!-- multi-targeting keeps both consumers working --> <PropertyGroup> <TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks> </PropertyGroup>
xml
<!-- conditional reference for one framework only --> <ItemGroup Condition="'$(TargetFramework)' == 'net8.0'"> <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" /> </ItemGroup>

Check what actually resolved with dotnet list package --include-transitive and compare the asset graph before and after a change with Diff.

3. Make usings explicit when implicit usings are off

ImplicitUsings adds a generated file with common namespaces. It is on by default for new .NET 6 and later projects and off for older ones, which is why the same file compiles in one repository and not another.

xml
<PropertyGroup> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup>
csharp
// If you cannot enable it, be explicit in the file using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

For a shared internal library, prefer explicit usings or a single GlobalUsings.cs with global using statements, so the dependency is visible in source control rather than in a generated file.

4. Clear the restore state before deeper debugging

The compiler reads resolved references from obj/project.assets.json, which is generated by restore. When the csproj is edited outside the IDE, or a branch switch changes package versions, that file can be stale.

bash
dotnet nuget locals all --clear rm -rf obj bin dotnet restore dotnet build -c Release
bash
# inspect the resolved reference graph for one project dotnet build -v:n 2>&1 | grep -i "warning NU\|error CS"

Most "it builds on my machine" CS0246 reports end here: a private feed inside NuGet.config is missing on the CI agent, so restore silently produced fewer assets.

5. Watch private assets and transitive references

A package referenced with PrivateAssets="all" does not flow to projects that reference yours. Their types disappear one level up, and the error appears in the consumer, not the library.

xml
<!-- library: consumers will not see this package's types --> <PackageReference Include="Some.Internal" Version="1.2.3" PrivateAssets="all" /> <!-- library: consumers can use the types, so keep the reference flowing --> <PackageReference Include="Some.Public" Version="1.2.3" />

If a public API exposes a type from a package, that package must not be private assets. Either make the type internal and wrap it, or let the reference flow.

Common Compilation/Runtime Mistakes & Version Gotchas

Adding the using when the package is missing. The compiler message lists both possibilities, and adding a using for an unreferenced assembly produces CS0246 again with no new information. Check the csproj first.

Assuming a namespace matches the assembly name. Microsoft.Extensions.Logging.Abstractions contains Microsoft.Extensions.Logging, and many packages place types in a nested namespace. Search inside the package rather than inferring from the package id.

Case and spelling. C# is case sensitive and Using/using at the top level is a directive. A file-scoped namespace written namespace App.Service; followed by usings placed after it is a compile error of a different kind, so keep usings above the namespace or inside a block namespace.

ImplicitUsings differences across SDKs. A project created with the .NET 8 SDK has it enabled; the same file copied into a .NET Framework style project does not. When porting, add the missing usings rather than enabling implicit usings in a project that did not use them.

Version differences worth knowing. .NET 6 introduced implicit usings and file-scoped namespaces; System.Text.Json ships in the shared framework from .NET Core 3.0 but needs a package reference on netstandard2.0. A package version that requires a newer SDK fails during restore with NU1202, not CS0246, so a CS0246 after a version bump usually means the reference was removed rather than upgraded.

Verification. Build the whole solution, not one project, because ProjectReference chains resolve differently per configuration. Then grep the source for the type name with Regex to see whether the consumer count matches your expectation; a type referenced from ten files and resolved in one is usually a namespace import that was only added once.

Reference material: CS0246 and MSBuild properties for the .NET SDK.

Official references

primary documentation

The C# documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.