RedundantOutVariable
Diagnostic Rule Overview
| Field | Value |
|---|---|
| ID | SHIMMER2020 |
| Analyzer title | Redundant out variable |
| Analyzer message | Out variable '0' is used exactly once and for assignment and therefore can be inlined |
| Code fix title | Inline temporary variable |
| Default severity | Info |
| Minimum framework/language version | C# 7 |
| Category | ShimmeringStyle |
| Link to code | RedundantOutVariableAnalyzer.cs |
| Code fix exists? | Yes |
Detailed Explanation
You can directly assign the value of an out parameter to a variable or a field without needing to declare a variable and then assign it.
Examples
Flagged code:
using System;
namespace Tests;
class Test
{
void Do(string dayOfWeekString)
{
if (Enum.TryParse(dayOfWeekString, out DayOfWeek dayOfWeek1))
{
DayOfWeek dayOfWeek2 = dayOfWeek1;
}
}
}
Fixed code:
using System;
namespace Tests;
class Test
{
void Do(string dayOfWeekString)
{
if (Enum.TryParse(dayOfWeekString, out DayOfWeek dayOfWeek2))
{
}
}
}
Justification of the Severity
This is a stylistic suggestion that is not related to performance or buggy behaviors.
When to Suppress
Suppress this diagnostic if you find the existing code more readable.