Is Resharper's recommendation to make my private method static a good recommendation?
c#, function, resharper, static
Solution
I think that's definitely a prime candidate for a static method. It's not changing any of the class's properties, fields, etc.
Here's an example:
class MyClass
{
public static void MakeStatusTheSame(MyClass mc, MySecondClass msc)
{
mc.status = msc.status;
}
private void MakeStatusTheSame(MySecondClass msc)
{
this.status = msc.status;
}
private int status;
}
Also, you could make it an extension method (which would also be static):
public static class Extensions
{
public static MyClass MakeStatusTheSame(this MyClass mc, MySecondClass msc)
{
mc.status = msc.status
return mc; /* make the method chainable */
}
}
Problem
I've recently noticed that when I create private methods that set a few fields in the objects passed to them that Resharper puts up a hint stating that the method can be made static. Here's a greatly simplified example of the sort of method I might have. ``` private void MakeStatusTheSame(MyClass mc, MySecondClass msc) { mc.Status = msc.Status; } ``` When I've got a method like this, Resharper provides a recommendation that the method can be made static. I try to avoid making public methods static since they wreck havoc on unit tests...but I'm not sure that the same applies for private methods. Is Resharper's recommendation a valid best practice or should I just turn it off?