Making Methods All Static in Class

前端 未结 10 1366
离开以前
离开以前 2021-01-04 12:10

I was told by my colleague based on one of my classes (it is an instance class) that if you have no fields in your class (backing fields), just make all methods static in th

10条回答
  •  无人及你
    2021-01-04 12:18

    There's no absolute rule for when a class should be static. It may have no state, but you may need it for reference equality or locking. Classes should be static when their purpose fits it being implemented as a static class. You shouldn't follow hard-and-fast rules in these situations; use what you 'feel' is right.

    Having no state makes it a candidate for static-ness, but look at what it's being used for before arbitarily refactoring it.

提交回复
热议问题