With the code below, Resharper raises a ‘virtual member call in constructor’ warning:
public class UserDetailViewModel : Screen
{
public UserDetailViewModel()
{
// DisplayName is a virtual member of Screen
DisplayName = "User Details";
}
}
Whereas if I change the code so it’s like this, the warning disappears:
public class UserDetailViewModel : Screen
{
public UserDetailViewModel()
{
SetName();
}
private void SetName()
{
DisplayName = "User Details";
}
}
Why does one raise a warning and the other not? Is the second way somehow correct, or is it just beyond the limit of what ReSharper can detect as potentially dangerous?
It’s just a limitation of ReSharper. It applies a bunch of heuristics to find code that it can warn about, but it won’t find everything. Doing so would require solving the halting problem. It’s not doable.
The lesson here is quite simple:
the absence of a warning does not mean that there is nothing to warn about.