Navigation Menu
Search code, repositories, users, issues, pull requests..., provide feedback.
We read every piece of feedback, and take your input very seriously.
Saved searches
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
- Notifications You must be signed in to change notification settings
Latest commit
File metadata and controls, remove unnecessary value assignment (ide0059).
:::zone pivot="lang-csharp-vb"
This rule flags unnecessary value assignments. For example:
You can take one of the following actions to fix this violation:
If the expression on the right side of the assignment has no side effects, remove the expression or the entire assignment statement. This improves performance by avoiding unnecessary computation.
If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a discard (C# only) or a local variable that's never used. Discards improve code clarity by explicitly showing the intent to discard an unused value.
The options for this specify whether to prefer the use of a discard or an unused local variable:
- C# - csharp_style_unused_value_assignment_preference
- Visual Basic - visual_basic_style_unused_value_assignment_preference
For information about configuring options, see Option format .
csharp_style_unused_value_assignment_preference
Visual_basic_style_unused_value_assignment_preference, suppress a warning.
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
To disable the rule for a file, folder, or project, set its severity to none in the configuration file .
To disable all of the code-style rules, set the severity for the category Style to none in the configuration file .
For more information, see How to suppress code analysis warnings .
:::zone-end
:::zone pivot="lang-fsharp"
This rule flags unnecessary value assignments. For example, answer is unused in the following snippet:
- Remove unused expression value (IDE0058)
- Code style rules reference
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to understand C# warning IDE0059: "Unecessary assignment"
I have the following scenario:
public void testmylife (ref string a) { string wstr = string.empty;
if (a == 1) { wstr = "Drop dead"; }
if (a == 2) { wstr = "Stay alive"; } }
I get IDE0059. How do I avoid that warning?
ASP.NET A set of technologies in the .NET Framework for building web applications and XML web services. 3,512 questions Sign in to follow Follow
as there is no code that uses the value wstr is set to, there is no point in setting a value to it. if you removed the unneeded assignments, you would get a warning about an unused variable.
your code snippet could be simplified to:
and function the same:
@Bruce (SqlWork.com) Thanks for the answer! For the sake of brevity, I left out some code in this snippet, but your answer helped me realize something about how I structure code and use variables.
I have this habit of initializing variables first, then as I wrote code, I might only use it inside the braces. This points out that I could just as easily start with a declaration inside the braces, and save myself leaving it unused at the top.
Again, thank you for helping me see this bad habit!
0 additional answers
Your answer.