ReSharper 2024.3 Help

Code inspection: Parameter output value is always discarded (private accessibility)

The C# 6.0 "out var" greatly reduces the amount of code needed to use methods with out parameters — now we almost always declare variables inline and often use type inference to omit variable type: dictionary.TryGetValue(key, out var value).

Another aspect of out parameters that C# 6.0 has improved is the ability to ignore the output parameter values that we don't need with M(out _) or M(out var _) syntax. So it's not uncommon to see invocations like DoSomething(arg, out _, out _) in modern C# codebase.

ReSharper analyzes such invocations and reports out parameters that are discarded on all call sites. This helps identify potential problems of a value being always discarded and simplify the signature by removing the unused parameters.

class Test { private Dictionary<string, int> _set = new(); // Parameter 'value' output value is always discarded private bool HasKey(string key, out int value) { return _set.TryGetValue(key, out value); } private void Usage(SetOfThings set) { if (set.HasKey("abc", out _)) Console.WriteLine("do something"); } }
class Test { private Dictionary<string, int> _set = new(); private bool HasKey(string key) { int value; return _set.TryGetValue(key, out value); } private void Usage(SetOfThings set) { if (set.HasKey("abc")) Console.WriteLine("do something"); } }
Last modified: 08 April 2024