Posts

Showing posts from August, 2020

Struct as Value Object

In C# you have two choices for representing value objects, either use class or struct.  In this article we look closer into struct as base building block for value objects. I would like to show you benefits and drawbacks of using structs so it will be easier to you to make decision what you should choose. Why are you want to make a value object as struct?  You can say for performance reasons. That makes sense as internally structures are allocated in memory stack. Thus structure allocation and deallocation is cheaper and faster operation in comparison to reference types.  On the other hand when you compare structures then you can fall into the trap. Default implementation of Equals and GetHashCode might not be so performant to you. Internally it can use reflection for comparing struct fields which may impact performance noticeably when you deal with huge collections or dictionaries. Fortunately it is enough to override Equals and GetHashCode and ideally implement IEquatable interface t