Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
| Property | Value |
|---|---|
| Rule ID | IDE0059 |
| Title | Remove unnecessary value assignment |
| Category | Style |
| Subcategory | Unnecessary code rules (expression-level preferences) |
| Applicable languages | C# and Visual Basic |
| Options | csharp_style_unused_value_assignment_preference |
visual_basic_style_unused_value_assignment_preference |
Overview
This rule flags unnecessary value assignments. For example:
// IDE0059: value written to 'v' is never
// read, so assignment to 'v' is unnecessary.
int v = Compute();
v = Compute2();
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.
int v = Compute2();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.
_ = Compute(); int v = Compute2();
Known limitations
This rule doesn't work in certain code contexts. The analyzer fails to flag unnecessary value assignments in the following scenarios:
- Assignments inside
tryorcatchblocks. - Assignments inside
usingstatement blocks. - Variables assigned within lambda expressions or delegate bodies.
- Variables in the presence of expression trees (
Expression<Func<T>>).
These limitations are tracked in the following GitHub issues:
- IDE0059 is not always reported
- Detection of IDE0059 not working within try/catch statements
- IDE0059 not raised inside a code block with using keyword
- IDE0059 doesn't work with expression trees
- Remove unused variable/values (parent issue)
Options
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
| Property | Value | Description |
|---|---|---|
| Option name | csharp_style_unused_value_assignment_preference | |
| Applicable languages | C# | |
| Option values | discard_variable |
Prefer to use a discard when assigning a value that's not used |
unused_local_variable |
Prefer to use a local variable when assigning a value that's not used | |
| Default option value | discard_variable |
// 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;
}
visual_basic_style_unused_value_assignment_preference
| Property | Value | Description |
|---|---|---|
| Option name | visual_basic_style_unused_value_assignment_preference | |
| Applicable languages | Visual Basic | |
| Option values | unused_local_variable |
Prefer to use a local variable when assigning a value that's not used |
| Default option value | unused_local_variable |
' visual_basic_style_unused_value_assignment_preference = unused_local_variable
Dim unused = Computation()
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.
#pragma warning disable IDE0059
// The code that's violating the rule is on this line.
#pragma warning restore IDE0059
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0059.severity = none
To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.
| Property | Value |
|---|---|
| Rule ID | IDE0059 |
| Title | The value is unused |
| Category | Style |
| Applicable languages | F# |
| Options | None |
Overview
This rule flags unnecessary value assignments. For example, answer is unused in the following snippet:
type T() =
let answer = 42