Static classes in C#, what's the pros/cons?

前端 未结 5 714
渐次进展
渐次进展 2021-01-13 12:23

I am programming a small game for my school assignment, the game is a simple 2D game with monsters, items and bullets. Basically you run around and tries to collect all the

相关标签:
5条回答
  • 2021-01-13 13:00

    Pros:

    • Easy access
    • Easy coding

    Cons:

    • No thread safety
    • No encapsulation
    • Reduced maintainability
    0 讨论(0)
  • 2021-01-13 13:01

    If your application is multithreaded you might need to think about the issues around multiple threads accessing and modifying static instances. Here's a good artical on threading:

    http://www.yoda.arachsys.com/csharp/threads/

    0 讨论(0)
  • 2021-01-13 13:05

    One advantage of static classes is that it is the only place you can define extension methods.

    0 讨论(0)
  • 2021-01-13 13:16

    I think this usage of a static class is fine - especially in a single-threaded application. Like others have said, your only alternative is to pass around some sort of contextual object that contains these lists.

    My personal opinion, in this instance, is that you have chosen the best solution for your problem. It is easy to implement, easy to use, and easy to change if need be down the road.

    0 讨论(0)
  • 2021-01-13 13:21

    I don't intend this as a serious answer but among the pro's, as you have discovered, is that it's very simple to get to your entities.

    Among the con's is that some people hate static classes so much that they will not hire you because you've used them. http://thegrenade.blogspot.com/2009/01/static-methods-and-classes-are-always.html

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