Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readC#
#csharp#cs0103#compiler-errors#namespaces
C# scope diagram with an identifier plate outside a namespace room and a using key beside it

Introduction & Quick Summary

A build that worked yesterday fails on one identifier:

code
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.

csharp
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:

  1. Locals and parameters in the enclosing block.
  2. Members of the containing type and its base types.
  3. Types in the enclosing namespace and its parents.
  4. Namespaces imported by using directives in the file or compilation unit.
  5. Global usings from the project (ImplicitUsings, <Using Include="...">).
  6. 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.

csharp
// 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:

csharp
// A.cs — no using, no implicit usings namespace Demo; public class Report { public string Serialize(object o) => JsonSerializer.Serialize(o); // CS0103 }
xml
<!-- 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

CauseTypical identifierFix
Missing usingJsonSerializer, Regex, Stopwatchadd the namespace import
Wrong namespaceyour own InvoiceServicealign namespace with folder or add using
Case or spellingconsole.WriteLinematch the declared name exactly
Conditional compilationa symbol inside #if DEBUGdefine the constant or move the code
Generated code missing[GeneratedRegex] methodrebuild so the generator runs
Wrong project referencea type from another projectadd ProjectReference and build it
Implicit usings disabledList<>, Taskenable or add explicit usings
SDK / templateImplicit global usings
Microsoft.NET.Sdk (net6.0+) with ImplicitUsings=enableSystem, System.IO, System.Linq, System.Net.Http, System.Threading, System.Threading.Tasks, collections
Microsoft.NET.Sdk.Webthe above plus Microsoft.AspNetCore.Builder, Hosting, Http, Routing
Microsoft.NET.Sdk.Workerthe base set plus hosting namespaces
netstandard2.0 or net48 style projectsnone
Name kindFirst place searchedCommon CS0103 trigger
local variablecurrent blockused before declaration in a switch arm
typeenclosing namespaces, then usingsimported in another file only
extension methodusings for the declaring namespaceusing System.Linq; missing
generated membersame type, after buildstale 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:

csharp
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:

csharp
// Services/InvoiceService.cs namespace Billing.Services; // matches the folder, matches the consumers

File-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:

xml
<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:

csharp
#if NET8_0_OR_GREATER builder.Services.AddOpenTelemetry(); #endif

When 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:

bash
dotnet clean dotnet build -v minimal

For 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:

xml
<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 using or a fully qualified reference.
  • Adding using inside a method. Using directives are file- or namespace-scoped; inside a method only using statements (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/ and bin/ as a first step. It fixes stale generated code sometimes, but costs a full restore; check the error's file path first.
  • global using collisions. Two project-wide imports that both define Task or Path produce CS0104, which is often mistaken for CS0103 when the message is skimmed.
  • Old-style projects. A net48 project 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.cs with class InvoiceService compiles 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 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.