How to Resolve Warning "CS0219: The variable 'variable' is assigned but its value is never used" in C#
The Compiler Warning CS0219 is a code quality warning. The message reads: "The variable 'variableName' is assigned but its value is never used".
This warning indicates dead code. You declared a variable and put data into it, but the compiler analyzed your code and found that you never read that data back. The variable serves no functional purpose in your current logic. This often happens during refactoring, debugging, or when calling methods that return values you don't actually need.
This guide explains how to clean up your code by identifying and removing these unnecessary assignments.
Understanding "Assigned but Not Used"
To the compiler, a variable's life has two main stages:
- Write (Assignment):
int x = 10; - Read (Usage):
Console.WriteLine(x);orint y = x + 5;
If you perform Step 1 without ever performing Step 2, the variable is useless. It consumes memory (stack space) and CPU cycles (for the assignment) but contributes nothing to the program's output.
CS0168 vs. CS0219:
- CS0168: Variable declared but never assigned (and never used).
- CS0219: Variable declared and assigned, but never read.
Scenario 1: Leftovers from Refactoring
This is the most common cause. You previously used a variable, but after changing the logic, you deleted the code that used it but forgot to delete the variable declaration itself.
Example of Code with Warning:
public void CalculateTotal(int a, int b)
{
// ⛔️ Warning CS0219: 'sum' is assigned value '30', but it is never used.
int sum = a + b;
// We are not returning 'sum' or printing it.
Console.WriteLine("Calculation complete.");
}
Solution: Delete the Variable
If the calculation has no side effects (like simple math), simply delete the line.
public void CalculateTotal(int a, int b)
{
// ✅ Correct: Removed the unused variable.
Console.WriteLine("Calculation complete.");
}
Scenario 2: Ignoring Method Return Values
Sometimes you call a method because it performs an action (like saving to a database), but that method also happens to return a value (like an ID or a success boolean). If you capture that return value into a variable but never check it, CS0219 triggers.
Example of error
public class UserRepo
{
public int SaveUser(string name)
{
// Logically saves user and returns ID
return 1;
}
}
public class Program
{
static void Main()
{
var repo = new UserRepo();
// ⛔️ Warning CS0219: 'userId' is assigned but never used.
// We wanted to run SaveUser(), but we don't care about the ID right now.
int userId = repo.SaveUser("Alice");
}
}
Solution: Call Without Assignment
You are not required to capture the return value of a method. You can simply call the method as a statement.
public class Program
{
static void Main()
{
var repo = new UserRepo();
// ✅ Correct: The method runs, the user is saved,
// and we simply ignore the returned integer.
repo.SaveUser("Alice");
}
}
Solution: Using Discards (_)
In some cases, C# syntax forces you to provide a variable, even if you don't want to use it. This is common with out parameters. Since C# 7.0, you can use the Discard symbol (_) to tell the compiler: "I know a value goes here, but I strictly do not care about it."
Example: out Parameters
public void ValidateNumber(string input)
{
// ⛔️ Warning CS0219: 'result' is assigned but never used.
if (int.TryParse(input, out int result))
{
Console.WriteLine("It is a valid number.");
}
}
Solution 1: Use Discard
public void ValidateNumber(string input)
{
// ✅ Correct: '_' tells the compiler we are ignoring the out parameter.
if (int.TryParse(input, out _))
{
Console.WriteLine("It is a valid number.");
}
}
Solution 2: Explicitly Ignoring Returns
You can also use assignment to discard _ = Method() to be explicit that you are ignoring a return value (this suppresses code analysis rules that might demand you check return values).
// ✅ Correct: Explicitly discarding the return value
_ = repo.SaveUser("Alice");
Conclusion
CS0219 is your "garbage collection" assistant for source code.
- Check Logic: Did you mean to use the variable? If so, write the code that uses it.
- Delete: If the variable is leftover from old code, delete it entirely.
- Call Directly: If capturing a method result you don't need, just call
Method();withoutvar x =. - Use Discards: If syntax forces a variable (like
out), useout _.