DTO classes vs. struct

后端 未结 1 1342
予麋鹿
予麋鹿 2021-02-12 09:10

So, this is actually this question is my current keystone. I\'m working on refactoring of my personal project, trying increase performance, optimize memory usage, make code easy

相关标签:
1条回答
  • 2021-02-12 09:20

    For standard DTO entities, you will want to stick with the class.

    A struct has a much more limited range of potential use cases than classes. There are also efficiency issues when struct types get too large (don't forget, they are value types and are copied when passed around), as outlined in the MSDN guidelines about value types. Not to mention plenty of gotchas when you start having struct types exposed through properties, or accidentally box it when referencing interfaces, or make them mutable...

    I'm not saying not to use struct when it is relevant, but I very rarely find myself needing to use struct types in our main desktop application - which is layered and features DTO types.


    The performance issue cannot be answered as simply as struct vs. class. You will need to employ a profiling tool such as dotTrace or ANTS in order to find the hotspots and go from there. Performance issues are not trivial and good tooling is usually the start of the answer.

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