ReSharper 2024.1 Help

Code inspection: Local variable hides primary constructor parameter

In the example below, the local variable str declared within the Print() method has the same name as the primary constructor parameter, but it is still clear that Console.WriteLine(str); will print out two.

Now imagine that the declaration and the invocation of str in Print() are separated by some lengthy code. In this case, readers of the code may miss the variable declaration and assume that Console.WriteLine(str); will print out one, received from the primary constructor parameter.

To avoid confusion and potential errors, rename the local variable.

public class MyTest { public MyTest() { var sample = new MySample("one"); } } public class MySample(string str) { void Print() { var str = "two"; Console.WriteLine(str); } }
Last modified: 11 February 2024