Should I prefer static methods in C#

前端 未结 6 1321
一整个雨季
一整个雨季 2021-02-05 08:47

Having spent a bit of time learning about functional programming, it\'s becoming more and more natural for me to want to work with static methods that don\'t perform any mutatio

6条回答
  •  醉梦人生
    2021-02-05 09:17

    For some reason I think of this quote when I read your question:

    You can write Fortran in any language.

    If the intent of C# were to be purely functional, static would be unnecessary because everything would be static by default. If you are strict about following OOP practices and the SOLID principles, your code effectively becomes functional (I know there's a quote out there about this somewhere) so you end up getting the best of both worlds.

    The reason I would curb it in a multi-user project would be that it's not typical C# (it's really C# with handcuffs). You just need one person to break the rule and declare a static mutable property and everything goes to hell.

提交回复
热议问题