error CS0103: The name '...' does not exist in the current context means the
compiler cannot see the name at that spot. Two causes cover most cases: the variable was
declared inside a block (try, if, a loop) and used outside it, or
the name is a type whose using directive is missing (or it is simply misspelt).
For a variable, declare it before the block and assign it inside. For a type, add the
using for its namespace. For a typo, compare the spelling and the
upper and lower case letters: C# treats Total and total as two
different names.
The error
...\Program.cs(10,34): error CS0103: The name 'text' does not exist in the current context [...\FixLab.csproj]
...\Program.cs(12,14): error CS0103: The name 'Regex' does not exist in the current context [...\FixLab.csproj]
Build FAILED.
The project folder was shortened to ...; nothing else was changed.
Why it happens
One small program produced both errors:
try
{
string text = File.ReadAllText("notes.txt");
}
catch (FileNotFoundException)
{
Console.WriteLine("notes.txt is missing");
}
Console.WriteLine($"Characters: {text.Length}");
bool valid = Regex.IsMatch("david.chen@example.test", @"^[^@\s]+@[^@\s]+$");
Console.WriteLine($"Valid email: {valid}");
Scope. A variable lives only inside the braces where it is declared.
text is declared inside the try block, so it no longer exists at
line 10. Wrapping a line in try is the classic way to meet this: the code
worked, you added error handling, and the next line broke.
A missing using. Regex lives in
System.Text.RegularExpressions. The implicit usings of a console project
are seven namespaces (System, System.Collections.Generic,
System.IO, System.Linq, System.Net.Http,
System.Threading, System.Threading.Tasks, listed in the
generated obj\Debug\net10.0\FixLab.GlobalUsings.g.cs), and that one is not
among them. Because
Regex is used as an expression here (a static method call), the compiler
reports CS0103 rather than the "type or namespace" error CS0246.
The fix
using System.Text.RegularExpressions;
string text = "";
try
{
text = File.ReadAllText("notes.txt");
}
catch (FileNotFoundException)
{
Console.WriteLine("notes.txt is missing");
}
Console.WriteLine($"Characters: {text.Length}");
bool valid = Regex.IsMatch("david.chen@example.test", @"^[^@\s]+@[^@\s]+$");
Console.WriteLine($"Valid email: {valid}");
text is now declared before the try with a starting value, so it
exists, and is definitely assigned, after the block; the using line brings
Regex into view. The project built with warnings as errors and printed
Characters: 39 and Valid email: True. If the name is a
variable you meant to declare once and reuse, check the spelling first; a wrong capital
letter is an easy typo to miss.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages) with the program above. dotnet build
reported the two CS0103 errors; the fixed version passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true and ran against a
three-line notes.txt.
Frequently asked
- Why does my variable not exist after a try block in C#?
- Variables declared inside braces exist only inside them. Declare the variable before the try block, give it a starting value, and assign the real value inside the try.
- Which using do I need for Regex in C#?
- using System.Text.RegularExpressions; It is not part of the implicit usings of a console project, so Regex is reported as CS0103 when you call one of its static methods.
- What is the difference between CS0103 and CS0246?
- CS0103 is reported for an unknown name used as a value or expression, such as a variable or a static call on a type. CS0246 is reported where a type is expected: without the using, the line Regex r = new Regex(...); gave CS0246 twice, once for each Regex.
When the missing name is a type in a declaration, the error is CS0246 instead; see CS0246: the type or namespace name could not be found. More are in the Fixes category.