2

Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?

Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.

Jawahar
  • 4,775
  • 1
  • 24
  • 47
  • You also may want to read https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil (which explains opposite point of view to "structs would be much cleaner" statement) – Alexei Levenkov Feb 02 '19 at 03:15
  • The simple answer is that in .NET, strutcs are value types. They are performant on the stack, but when you are trying to use them as data holders and pass them over from one context to another or reference them from a class, boxing is required to have them moved on the heap. Boxing is inefficient so classes are preferred since they live on the heap and are not subject to boxing. – Cosmin Sontu Feb 02 '19 at 07:57

1 Answers1

0

In C#, struct type is a value, so looks that it is not a good choice.