Which is best for data store Struct/Classes?

后端 未结 9 1871
无人共我
无人共我 2020-11-30 05:29

We have seen lots of discussion in SO regarding the class vs struct in c#. Mostly ended with conclusions saying its a heap/stack memory allocation. And reco

9条回答
  •  失恋的感觉
    2020-11-30 06:25

    I can never really seem to remember, exactly how structs are different, but they are. In subtle ways. In fact, sometimes they come and bite you.

    So. Unless you know what you are doing, just stick to classes.

    I know this sounds a little newbie. I know I should right now go and look up the differences and display them here - but that has already been done by others. All I'm saying is that adding a different type of objects creates a semantical burden, a bit of extra complexity that you are wise to consider carefully.

    If I remember correctly, one of the biggest problem is the value semantics of structs: Passing them around will result in different objects (as they get passed by value). If you then change some field in one place, beware that in all other places the field did not get changed! That is why everyone is recommending immutability for structs!

    EDIT: For the case you are describing, structs won't work!

提交回复
热议问题