Method can be made static, but should it?

后端 未结 14 1092
说谎
说谎 2020-11-22 17:16

Resharper likes to point out multiple functions per asp.net page that could be made static. Does it help me if I do make them static? Should I make them static and move them

相关标签:
14条回答
  • 2020-11-22 17:51

    You should do what is most readable and intuitive in a given scenario.

    The performance argument is not a good one except in the most extreme situations as the only thing that is actually happening is that one extra parameter (this) is getting pushed onto the stack for instance methods.

    0 讨论(0)
  • 2020-11-22 17:51

    Just my tuppence: Adding all of the shared static methods to a utility class allows you to add

    using static className; 
    

    to your using statements, which makes the code faster to type and easier to read. For example, I have a large number of what would be called "global variables" in some code I inherited. Rather than make global variables in a class that was an instance class, I set them all as static properties of a global class. It does the job, if messily, and I can just reference the properties by name because I have the static namespace already referenced.

    I have no idea if this is good practice or not. I have so much to learn about C# 4/5 and so much legacy code to refactor that I am just trying to let the Roselyn tips guide me.

    Joey

    0 讨论(0)
提交回复
热议问题