All rules
IDE0059Unnecessary code rules (expression-level preferences)

The value is unused

The value is unused

Microsoft docs

Description

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.

Example

// csharp_style_unused_value_assignment_preference = discard_variable
int GetCount(Dictionary<string, int> wordCount, string searchWord)
{
    _ = wordCount.TryGetValue(searchWord, out var count);
    return count;
}

// csharp_style_unused_value_assignment_preference = unused_local_variable
int GetCount(Dictionary<string, int> wordCount, string searchWord)
{
    var unused = wordCount.TryGetValue(searchWord, out var count);
    return count;
}

Configurable options

Vote for the value each option should take in the generated .editorconfig.

csharp_style_unused_value_assignment_preference
default: discard_variable
visual_basic_style_unused_value_assignment_preference
default: unused_local_variable
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0