CS0168
C# Programming Language
Severity: MinorWhat Does This Error Mean?
CS0168 is a compiler warning that tells you a variable was declared but its value was never read. For example: 'int count = 0;' but 'count' is never used anywhere after that. This is not a crash — your program still runs. But unused variables are wasted memory and a sign that something may have been forgotten or removed incompletely.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Visual Studio
- Visual Studio Code
- Rider
Common Causes
- A variable was declared during development for testing but never removed
- Code was refactored and the variable is no longer needed, but the declaration was left in
- A variable was declared for a calculation that was later changed to use a different approach
- A method's out parameter is captured in a variable but the variable is never read
- Copy-pasting code brought in a variable declaration that does not apply in the new context
How to Fix It
-
Find the variable named in the warning. Search the surrounding code to confirm it is truly never read — not just never written to.
A variable may be assigned a value (written to) but never read back. Both patterns trigger this warning.
-
If the variable is genuinely not needed, delete the declaration. Also delete any assignments to it if those assignments have no side effects.
Be careful: if the right-hand side of an assignment calls a method with side effects, you may need to keep the call but drop the assignment.
-
If the variable was meant to be used, find the code that should use it and add the missing usage. Perhaps a function call is missing a parameter, or a result is being discarded by mistake.
Sometimes this warning reveals a real bug — a value was calculated and stored but the code forgot to actually use it.
-
For out parameters you want to ignore (for example from int.TryParse), use the discard symbol _ in C# 7 and later.
Example: 'int.TryParse(input, out _);' — the _ tells C# you intentionally do not need the output value. No warning, no waste.
-
Review catch blocks. A common pattern is 'catch (Exception ex)' where 'ex' is never used. Either use ex to log the error, or change it to 'catch (Exception)' without the variable name.
Swallowing exceptions without logging them is a bad practice anyway. Use the exception variable to log meaningful error information.
When to Call a Professional
CS0168 is a warning you can always fix yourself — just remove the unused variable. If you are not sure why a variable was there, check your version control history for context. For unused out parameters specifically, C# 7 and later allows you to use the discard '_' to intentionally ignore them.
Frequently Asked Questions
Is an unused variable actually harmful?
It is not harmful at runtime — the variable takes a tiny amount of memory and the compiler usually optimizes it away. However, it is harmful to code quality. Unused variables clutter the code, confuse readers, and can hide logical errors where a result was calculated but the code forgot to use it. Treating warnings as errors encourages writing clean, intentional code.
What is the discard symbol _ and when should I use it?
The discard _ is a special variable name that means 'I intentionally do not care about this value'. Use it for out parameters you do not need: 'bool isValid = int.TryParse(input, out _);' Use it in deconstruction when you only want some parts: 'var (name, _) = GetPersonInfo();' It signals intent clearly — both to the compiler and to anyone reading the code.
How do I set up my project to treat warnings as errors?
In your .csproj file, add '<TreatWarningsAsErrors>true</TreatWarningsAsErrors>' inside a PropertyGroup. This makes all warnings fail the build, forcing you to fix them. Alternatively, use '<WarningsAsErrors>CS0168,CS0162</WarningsAsErrors>' to only treat specific warnings as errors. This is a great practice for new projects — it prevents warning debt from accumulating.