C# CS0103: The name does not exist in the current context
CS0103 means the identifier is not in scope where used. Add the using directive, fix the namespace, check casing, and rebuild so generated code exists.

Introduction & Quick Summary
A build that worked yesterday fails on one identifier:
Services/InvoiceService.cs(41,17): error CS0103: The name 'JsonSerializer'
does not exist in the current context [C:\src\Billing\Billing.csproj]Quick answer: the compiler cannot resolve that identifier where it appears. CS0103 is a scope error, not a missing package: the type or variable exists somewhere, but not in the namespace or block that references it.
using System.Text.Json; // the using directive was missing
namespace Billing.Services;
public class InvoiceService
{
public string ToJson(Invoice invoice) =>
JsonSerializer.Serialize(invoice);
}Work through four questions in order: is the type's namespace imported (using), is the file in the namespace you think it is (namespace and folder agree), does the name match exactly (C# is case-sensitive), and does the referenced project or package actually build with a compatible target framework.
This guide explains how C# resolves a name, why implicit usings change which errors you see, and the six situations — missing using, namespace drift, conditional compilation, source generators, Razor, and old project formats — that produce CS0103 with very different fixes.
Core Mechanics, Signatures & Reproducible Setup
C# resolves an identifier by searching outward from the point of use:
- Locals and parameters in the enclosing block.
- Members of the containing type and its base types.
- Types in the enclosing namespace and its parents.
- Namespaces imported by
usingdirectives in the file or compilation unit. - Global usings from the project (
ImplicitUsings,<Using Include="...">). - Types in referenced assemblies that are visible through the above.
If none of those contain the name, the compiler reports CS0103. Nothing about the package is considered until the name is visible; that is why installing a NuGet package alone never fixes this error.
// Program.cs — compiles only because the SDK adds global usings
var builder = WebApplication.CreateBuilder(args); // Microsoft.AspNetCore.Builder
var app = builder.Build();
app.Run();A minimal reproduction of each common cause:
// A.cs — no using, no implicit usings
namespace Demo;
public class Report
{
public string Serialize(object o) => JsonSerializer.Serialize(o); // CS0103
}<!-- Demo.csproj: ImplicitUsings off -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>With ImplicitUsings enabled in an SDK-style project, System, System.Collections.Generic, System.Linq, System.Threading and System.Threading.Tasks are imported for you, and for web projects the ASP.NET Core namespaces are added as well. Disabling it turns every file that relied on them into CS0103 candidates.
Key Comparison Table
| Cause | Typical identifier | Fix |
|---|---|---|
Missing using | JsonSerializer, Regex, Stopwatch | add the namespace import |
| Wrong namespace | your own InvoiceService | align namespace with folder or add using |
| Case or spelling | console.WriteLine | match the declared name exactly |
| Conditional compilation | a symbol inside #if DEBUG | define the constant or move the code |
| Generated code missing | [GeneratedRegex] method | rebuild so the generator runs |
| Wrong project reference | a type from another project | add ProjectReference and build it |
| Implicit usings disabled | List<>, Task | enable or add explicit usings |
| SDK / template | Implicit global usings |
|---|---|
Microsoft.NET.Sdk (net6.0+) with ImplicitUsings=enable | System, System.IO, System.Linq, System.Net.Http, System.Threading, System.Threading.Tasks, collections |
Microsoft.NET.Sdk.Web | the above plus Microsoft.AspNetCore.Builder, Hosting, Http, Routing |
Microsoft.NET.Sdk.Worker | the base set plus hosting namespaces |
netstandard2.0 or net48 style projects | none |
| Name kind | First place searched | Common CS0103 trigger |
|---|---|---|
| local variable | current block | used before declaration in a switch arm |
| type | enclosing namespaces, then usings | imported in another file only |
| extension method | usings for the declaring namespace | using System.Linq; missing |
| generated member | same type, after build | stale obj/ folder |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Add the namespace, not the package
JsonSerializer lives in System.Text.Json; Regex in System.Text.RegularExpressions; Stopwatch in System.Diagnostics. Use the IDE's quick action (Ctrl+.) or write it explicitly:
using System.Text.Json;
using System.Text.Json.Serialization;The BCL moved several types between assemblies across versions while keeping namespaces stable, so a package reference is rarely the missing piece for CS0103 — an assembly reference or using is.
2. Namespace and folder drift
A file moved into Services/ while its namespace still says Billing.Data compiles fine on its own and fails at every use site:
// Services/InvoiceService.cs
namespace Billing.Services; // matches the folder, matches the consumersFile-scoped namespaces (C# 10+) remove one level of brace mistakes. When a class is found in a tests project but not in the app, check that the namespace declaration is inside the file's only namespace block rather than nested in an unrelated one.
3. Implicit usings and global usings
<ImplicitUsings>enable</ImplicitUsings> injects a generated file with global using lines. You can add your own project-wide imports the same way:
<ItemGroup>
<Using Include="System.Text.Json" />
<Using Include="Billing.Domain" />
<Using Include="Microsoft.Extensions.Logging" />
</ItemGroup>This is convenient and easy to overuse: a project-wide using for a namespace with a common type name (Task, Timer, Path) causes ambiguity errors (CS0104) in files that also import another namespace with the same type.
4. #if and target-framework symbols
Code guarded by a symbol that is not defined simply does not exist for the compiler:
#if NET8_0_OR_GREATER
builder.Services.AddOpenTelemetry();
#endifWhen the project multitargets net8.0;net48, the net48 pass sees none of that code, and call sites that assume it exists fail with CS0103. Check the active configuration in the build output (-f net8.0) and the <DefineConstants> in the .csproj, not just the source file.
5. Source generators and Razor
Generated members exist only after the generator runs. A clean build usually fixes a CS0103 that points at [GeneratedRegex], System.Text.Json source-generated contexts, or a Razor page's Model property:
dotnet clean
dotnet build -v minimalFor Razor, a missing @using in _ViewImports.cshtml produces CS0103 in the generated .g.cs file; the error's file path (obj/Debug/net8.0/...) is the clue that the fix belongs in the view imports, not in the C# file.
6. Project references and target frameworks
A type from a referenced project is invisible when the reference is missing or the framework is incompatible:
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>If Domain targets net8.0 and the web project targets net48, the build reports NU1201 or a framework error first; when only some files fail with CS0103, confirm that the class is public and not internal in the referenced assembly.
The regular expression tester helps extract every CS0103 identifier and file from a long build log, and the text diff tool shows what changed in a .csproj between the commit that built and the one that did not.
Common Compilation/Runtime Mistakes & Version Gotchas
- Adding a NuGet package to fix CS0103. A package brings assemblies; the name still needs a
usingor a fully qualified reference. - Adding
usinginside a method. Using directives are file- or namespace-scoped; inside a method onlyusingstatements (disposal) are legal. - Fixing one file and not the other. A namespace rename leaves half the project compiling; use "Rename" on the namespace symbol rather than editing text.
- Deleting
obj/andbin/as a first step. It fixes stale generated code sometimes, but costs a full restore; check the error's file path first. global usingcollisions. Two project-wide imports that both defineTaskorPathproduce CS0104, which is often mistaken for CS0103 when the message is skimmed.- Old-style projects. A
net48project with packages.config has no implicit usings and no SDK defaults, so every namespace must be imported explicitly. - Case-only differences on case-insensitive file systems. A file saved as
Invoiceservice.cswithclass InvoiceServicecompiles on Windows and fails after a move to a Linux build agent only if the reference is by file name; check the class name, not the file name, for CS0103.
Official documentation: CS0103 compiler error and using directive.
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.
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.
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.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.