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 over this field, you’ll get the following message:
![ReSharper detects unused field ReSharper detects unused field](https://resources.jetbrains.com/help/img/dotnet/2024.3/cookbook_dead_code.png)
If you now press AltEnter over the _name
field, ReSharper will offer an option to remove the name field and any assignment to it:
![ReSharper detects unused field ReSharper detects unused field](https://resources.jetbrains.com/help/img/dotnet/2024.3/cookbook_dead_code1.png)
And here is the result:
![ReSharper detects unused field ReSharper detects unused field](https://resources.jetbrains.com/help/img/dotnet/2024.3/cookbook_dead_code2.png)
Notice that now ReSharper informs us that the parameter can also be removed. We use the corresponding quick-fix:
![ReSharper detects unused field ReSharper detects unused field](https://resources.jetbrains.com/help/img/dotnet/2024.3/cookbook_dead_code3.png)
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 ReSharper detects unused field](https://resources.jetbrains.com/help/img/dotnet/2024.3/cookbook_dead_code4.png)