Skip to main content

How to Resolve Warning "CS0168: The variable 'var' is declared but never used" in C#

The Compiler Warning CS0168 is a code quality and cleanliness warning. The message reads: "The variable 'variableName' is declared but never used".

This warning indicates that you have defined a local variable inside a method, possibly implicitly initialized it (e.g., int x;), but the rest of your code never reads from it or assigns a value to it. While this does not prevent your application from compiling or running, it indicates Dead Code. Unused variables clutter the code, consume a tiny amount of stack memory, and can sometimes confuse other developers who might wonder why the variable exists.

This guide explains common causes for this warning and how to resolve it using deletion, generic catch blocks, or discards.

Understanding "Used" vs. "Declared"

The compiler analyzes the flow of your method.

  • Declared: You allocated space for the variable (int x;).
  • Used: You read the value (int y = x + 1;) or passed it to a method (Print(x);).

If you declare a variable but never touch it again, the compiler flags it with CS0168.

note

CS0168 vs CS0219:

  • CS0168: Variable declared, never assigned, never read.
  • CS0219: Variable assigned a value, but the value is never read. The solution for both is generally the same: remove the variable.

Scenario 1: Leftover Variables from Refactoring

The most common cause is changing the logic of a method but forgetting to delete the old variables that are no longer needed.

Example of error

public void ProcessData()
{
// ⛔️ Warning CS0168: The variable 'count' is declared but never used.
int count;

Console.WriteLine("Processing started...");
// We might have used 'count' before, but now the logic is gone.
PerformWork();
}

Solution: Delete the Variable

Simply remove the declaration line.

public void ProcessData()
{
// ✅ Correct: Dead code removed.
Console.WriteLine("Processing started...");
PerformWork();
}

Scenario 2: Unused Exception Variables in Catch Blocks

This is extremely common. You write a try-catch block to handle errors, and you declare the exception variable ex, but you never log it or inspect it.

Example of error

public void ReadFile(string path)
{
try
{
var text = System.IO.File.ReadAllText(path);
}
// ⛔️ Warning CS0168: The variable 'ex' is declared but never used.
catch (System.IO.IOException ex)
{
Console.WriteLine("Could not read file.");
// We didn't use 'ex.Message' or pass 'ex' to a logger.
}
}

Solution: Remove the Variable Name

You can catch a specific exception type without declaring a variable for it.

public void ReadFile(string path)
{
try
{
var text = System.IO.File.ReadAllText(path);
}
// ✅ Correct: We catch IOException, but we don't create a variable.
catch (System.IO.IOException)
{
Console.WriteLine("Could not read file.");
}
}

Scenario 3: Unused Out Parameters

Sometimes you call a method that requires an out parameter (like TryParse), but you only care about the return value (success/failure), not the actual result.

Example of error

public void ValidateInput(string input)
{
// ⛔️ Warning CS0168: The variable 'result' is declared but never used.
int result;

if (int.TryParse(input, out result))
{
Console.WriteLine("It is a valid number.");
}
}

Solution: Use Discards (C# 7.0+)

Use the underscore _ (Discard) to tell the compiler: "I know this method outputs a value, but I intentionally want to ignore it."

public void ValidateInput(string input)
{
// ✅ Correct: 'out _' creates a discard. No variable is allocated.
if (int.TryParse(input, out _))
{
Console.WriteLine("It is a valid number.");
}
}

Conclusion

CS0168 helps keep your codebase clean.

  1. Standard Variables: If you aren't using it, delete it.
  2. Catch Blocks: If you don't need to read the exception details, remove the variable name ex and keep only the type.
  3. Out Parameters: Use the discard out _ syntax to ignore required output parameters.