Code inspection: Potentially misleading parameter name in lambda or local function
Historically, the symbol _ has been a valid identifier name in C#, making it perfectly legal to name variables, parameters, and other code entities as _. However, with the introduction of the discards concept in C#, using _ as a name has become undesirable, as it can block the usage of _ to express the value discard in some scope of code.
As C# gradually moves towards limited breaking changes to address this issue, this inspection helps future-proof your code and minimize potential confusion. It reports variables and parameters named _ that are actually being used, making them appear as a 'discarded' value.
If a parameter or a variable name consists of multiple _ characters and has usages, it will also be reported.
public class Test
{
private Action<string> greet = _ =>
{
Console.WriteLine("Hello " + _);
};
}
public class Test
{
private Action<string> greet = name =>
{
Console.WriteLine("Hello " + name);
};
}
Last modified: 14 May 2024