Should I go with static methods or non static methods?

那年仲夏 提交于 2019-12-01 22:59:39

You shouldn't be deciding whether or not to use static fields/methods based on memory consumption (which likely won't be altered much). Instead, you should go with what produces cleaner, more testable code.

Static methods are okay (IMO) if you don't need any kind of polymorphic behaviour, and if the method doesn't logically act on an instance of the type. However, if you've also got static variables involved, that's more of an issue. Static variables - other than constants - can make code much harder to test, reuse, and handle correctly in multiple threads.

It sounds like you probably should be using instance variables and methods. Just make your Main method create an instance of the class, and it can use that instance to create delegates to pass to the timer. It's hard to be much more precise than that without knowing more about what you're doing, but it does sound like you're using statics for immediate convenience rather than because it's the right thing to do, which is always a worry.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!