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.

Introduction & Quick Summary
The compiler cannot resolve a symbol, so it stops on the first use:
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
usingstatement names the wrong namespace or has a typo. - A project reference was removed during a refactor.
- A file-scoped namespace or
global usingwas assumed butImplicitUsingsis 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.
<!-- Broken.csproj: package missing -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>// 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;
}$ dotnet build
Broken.cs(7,30): error CS0246: The type or namespace name 'ILogger<>' could not be foundThe fix is one reference, and the build then resolves the type through the shared framework's extensions assemblies:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
</ItemGroup>Neighbouring errors and what each tells you:
| Code | Message | Meaning |
|---|---|---|
| CS0246 | type or namespace name could not be found | no type with that name is referenced |
| CS0234 | type or namespace does not exist in the namespace | the namespace exists, the member does not |
| CS0103 | name does not exist in the current context | a variable or member, not a type |
| CS1061 | does not contain a definition for | the type resolved, the member did not |
| CS0012 | type is defined in an assembly that is not referenced | a dependency's dependency is missing |
| NETSDK1004 | assets file not found | restore never ran |
Key Comparison Table
| Where the type lives | Reference to add | Where it goes |
|---|---|---|
| NuGet package | <PackageReference Include="..." Version="..." /> | ItemGroup in the csproj |
| Another project in the solution | <ProjectReference Include="..." /> | ItemGroup in the csproj |
| Framework assembly | change <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:
| Symptom | Cause | Check |
|---|---|---|
IDE shows no error, dotnet build fails | restore skipped or a stale obj folder | dotnet restore, delete obj and bin |
| Fails in CI only | a NuGet.config with a private feed, or a locked mode restore | dotnet restore --locked-mode output |
| Fails after a target framework change | the package has no assets for the new framework | dotnet list package --include-transitive |
| Fails in one file only | a using inside a namespace block | the file's namespace and usings |
| Fails with a valid package | package is marked private assets | the 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.
dotnet package search Microsoft.Extensions.Logging --take 5
dotnet add package Microsoft.Extensions.Logging.Abstractionsusing 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.
<!-- multi-targeting keeps both consumers working -->
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup><!-- 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.
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>// 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.
dotnet nuget locals all --clear
rm -rf obj bin
dotnet restore
dotnet build -c Release# 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.
<!-- 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 documentationThe C# documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# CS0117: 'X' does not contain a definition for 'Y'
CS0117 means the compiled assembly has no such member, usually from an older target framework or package. Check the API surface before editing code.
CS1061: does not contain a definition for and no extension method
Member lookup failed on both instance and extension methods. Type inference, missing using directives, target frameworks and packages explain most cases.
C#: NullReferenceException: Object reference not set
The message never names the null. Read the stack trace, enable nullable reference types, and use null-conditional access instead of guessing.
.NET: The framework 'Microsoft.NETCore.App' was not found
A framework-dependent app asks for a runtime major that is absent. Install the matching runtime or publish self-contained, and match Docker tags.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.