ReSharper 2017.1 Help

Code Inspection: Private field can be converted to local variable

ReSharper helps you respect the principle of locality: if a field is only assigned and used in a local scope, ReSharper suggests to convert the field to a local variable.

First, this transformation will normally reduce memory usage (however, this effect may be minimized by JIT optimization). Second, it will improve readability of your code.

Here is an example of a quick-fix suggested by this inspection:

Suboptimal codeAfter the quick-fix
public class Foo { private int _index; public Foo(string name) { _index = name.LastIndexOf('.'); Console.WriteLine(_index); } }
public class Foo { public Foo(string name) { int index = name.LastIndexOf('.'); Console.WriteLine(index); } }
Last modified: 12 October 2017

See Also