Skip to main content

RedundantOutVariable

Diagnostic Rule Overview

FieldValue
IDSHIMMER2020
Analyzer titleRedundant out variable
Analyzer messageOut variable '0' is used exactly once and for assignment and therefore can be inlined
Code fix titleInline temporary variable
Default severityInfo
Minimum framework/language versionC# 7
CategoryShimmeringStyle
Link to codeRedundantOutVariableAnalyzer.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.