Why does Visual Studio by default create a private static method when refactoring code and selecting extract method?
If I'm refactoring a non-static class and the method is only visible within the class why is it static?
Is there some performance benefit by calling a private static method within a non-static class compared to a non-static method within a non-static class?
Is it for readability to simply show that the method does not rely on any member variables of the class?
Why does Visual Studio by default create a private static method when refactoring code and selecting extract method?
It does this only if your method doesn't access any member variables/methods/properties. This is good because it basically operates on the principle of least assumptions: since you don't access instance-specific data, might as well make the method static
.
Is there some performance benefit by calling a private static method within a non-static class compared to a non-static method within a non-static class?
Theoretically, there might be but I doubt it. However, making the method static makes it clear that it will not access or modify instance data, which I find a useful hint.
Creating a static method may be considered a performance enhancement because there is no "this" pointer to pass as a variable. I use ReSharper and it always recommends turning methods into static whenever they don't refer to a class variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.