use MORE structs?

前端 未结 3 1708
心在旅途
心在旅途 2021-01-20 00:57

There have been several questions over the past few days about the proper use of null; here are three (one is mine):

  • Best Practice: Should functions r
相关标签:
3条回答
  • 2021-01-20 01:41

    I think you should just stick to the Null Object, being creative like this will just get you into trouble, and probably make the code less maintainable.

    0 讨论(0)
  • 2021-01-20 01:50

    The big limitation (IMO) of a struct is that it ought to be immutable.

    I have several times defined and used a user-defined struct, for the reason you suggested (ie. because a struct can't be null); but I was then often (until I learned to never make them anything but immutable) burned by modifying a copy (sometimes an unnamed temporary copy) of a struct instance, instead of modifying the instance itself.

    0 讨论(0)
  • 2021-01-20 01:51

    If you start using structs in weakly types scenarios (like assigning to an object type) Object o = structValue; or Object o = new mystruct(); or Object o = default(mystruct); etc. the values are boxed onto the heap, and they are also unboxed when used. This is a potential drawback because it affects performance. See info from Microsoft about boxing and unboxing.

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