ReSharper 2017.1 Help

Find dead .NET code

One benefit of using ReSharper is that it shows you bits of your own code that you’re not using. ReSharper is actually very smart about it: not only will it point out code that isn’t used anywhere, but it will detect more complicated cases – for example the case where a field is both declared and initialized, but nothing apart from initialization actually happens to it.

Let’s take a look at an example. Say we have a class Person defined as follows:

public class Person { private string _name; public Person(string name) { this._name = name; } }

Now, ReSharper will indicate that the _name field isn’t used. If you hover the mouse pointer over this field, you’ll get the following message:

ReSharper detects unused field

If you now press Alt+Enter over the _name field, ReSharper will offer an option to remove the name field and any assignment to it:

ReSharper detects unused field

And here is the result:

ReSharper detects unused field

Notice that now ReSharper informs us that the parameter can also be removed. We use the corresponding quick-fix:

ReSharper detects unused field

And we get an empty constructor which, as you may have guessed, is also not needed, since a default constructor is implemented by default. Once again, ReSharper picks up on it and offers us an option to get rid of the constructor:

ReSharper detects unused field

See Also

Last modified: 4 July 2017