How to Resolve Error "CS0165: Use of unassigned local variable 'name'" in C#
The Compiler Error CS0165 is a Definite Assignment error. The message reads: "Use of unassigned local variable 'variableName'".
In C#, local variables (variables declared inside a method) are not automatically initialized to a default value (unlike class fields). The compiler enforces a strict rule: you must explicitly assign a value to a local variable before you attempt to read it or pass it to a method. If there is any possible path through your code where the variable might be accessed without having a value, the compiler blocks the build.
This guide explains how to ensure your variables are always assigned before use.
Understanding Local vs. Field Initialization
- Class Fields: If you declare
private int _count;inside a class, C# automatically sets it to0. - Local Variables: If you declare
int count;inside a method, it contains garbage/undefined data. C# refuses to read this undefined memory for safety reasons.
To fix CS0165, you must guarantee that the variable has a value before line X, where line X is trying to read it.
Scenario 1: Declaration Without Initialization
The simplest form of this error is declaring a variable and trying to print it immediately.
Example of error
public void PrintScore()
{
// Declared, but holds no value
int score;
// ⛔️ Error CS0165: Use of unassigned local variable 'score'
System.Console.WriteLine(score);
}
Solution: Assign a Value
Initialize the variable on the declaration line or before usage.
public void PrintScore()
{
// ✅ Correct: Initialized to 0
int score = 0;
System.Console.WriteLine(score);
}
Scenario 2: Conditional Initialization (The 'if' Trap)
This is the most frequent cause. You assign a value inside an if block, but you use the variable after the if block. The compiler worries: "What if the condition is false?"
Example of error
public void CheckAccess(bool isAdmin)
{
string message;
if (isAdmin)
{
message = "Welcome, Admin";
}
// If isAdmin is false, 'message' is never assigned!
// ⛔️ Error CS0165: The compiler sees a path where 'message' is empty.
System.Console.WriteLine(message);
}
Solution: Provide an Else or a Default
You must ensure assignment happens in all paths.
Option A: Assign Default at Declaration
public void CheckAccess(bool isAdmin)
{
// ✅ Correct: Start with a default value (e.g., null or string.Empty)
string message = "Access Denied";
if (isAdmin)
{
message = "Welcome, Admin";
}
System.Console.WriteLine(message);
}
Option B: Use an Else Block
public void CheckAccess(bool isAdmin)
{
string message;
if (isAdmin)
{
message = "Welcome, Admin";
}
else
{
// ✅ Correct: Now all paths assign a value
message = "Access Denied";
}
System.Console.WriteLine(message);
}
Scenario 3: Try/Catch Blocks
If you define a variable outside a try block, assign it inside the try, and attempt to use it after the catch, the compiler flags an error. This is because if an exception occurs before the assignment line, the variable remains unassigned.
Example of error
public void ReadFile()
{
string content;
try
{
// If ReadAllText throws, the assignment never happens
content = System.IO.File.ReadAllText("data.txt");
}
catch
{
System.Console.WriteLine("Failed to read.");
}
// ⛔️ Error CS0165: If the catch block ran, 'content' is empty.
System.Console.WriteLine(content);
}
Solution: Initialize Outside
Initialize the variable before entering the try block.
public void ReadFile()
{
// ✅ Correct: Default to null or empty
string content = string.Empty;
try
{
content = System.IO.File.ReadAllText("data.txt");
}
catch
{
System.Console.WriteLine("Failed to read.");
// content remains string.Empty here
}
System.Console.WriteLine(content);
}
Structs:
If you are using a struct (like Point), CS0165 will trigger if you assign only some of its fields. You must either assign all fields individually or initialize the struct using new Point().
Conclusion
CS0165 ensures code safety by preventing access to uninitialized memory.
- Check Declarations: Did you forget
= value;? - Check Branches: Does your
if,switch, ortrylogic cover every possible scenario? - The Fix: The easiest fix is usually initializing the variable to a default value (
null,0,false) right where you declare it.