Version: SonarLint 6.12.0.59751 / Visual Studio / not connected
Rule: C# / S1172
The rule (do not include unused parameters) is triggered with this code:
private static void BuildSection(
IReadOnlyDictionary<int, string> items, // <- considered unused
IEnumerable<int> values
)
{
var groups = values.Select(items.GetValueOrDefault); // <- actually used here
// ...
}
This version of the code does NOT trigger the rule:
private static void BuildSection(
IReadOnlyDictionary<int, string> items,
IEnumerable<int> values
)
{
var groups = values.Select(v => items.GetValueOrDefault(v));
// ...
}
The difference is that the first version passes a method directly as the argument of .Select()
, while the second wraps it in a lambda.
Note that GetValueOrDefault
also has an overload with two parameters: the compiler seems to pick the correct one in the first case, but i guess Sonar gets confused about them.
See here: CollectionExtensions.GetValueOrDefault Method (System.Collections.Generic) | Microsoft Learn