Quote: Original post by Strewth
I've been using C# for probably 3 or 4 years now and have never felt the need to use a Struct. What circumstances warrant it?
Structures are not strictly heap allocated. By default, creating an instance of a structure within a method will result in it being created on the stack, and released from the stack when the method ends. Furthermore, structures are value types. You use them all the time, integers and floats being two prime examples of structures you use frequently. As they are value types, they are passed by value, which means that large structures, when passed, will result in a large amount of copying.
Structures are ideal to use in instances where having small heap allocations standing around will increase GC counts and decrease performance. For instance, matrix multiplication used in games for transformations typically results in large numbers of temporary matrix instances laying around. If those were heap allocated, then it could push other slightly longer lived temporaries up into gen1, which would end up resulting in slower collections by the GC (since it will have to clean up gen1 stuff as well). There are other reasons as well, sometimes it just makes sense. A complex type would be best represented as a struct, since that most readily expresses its usage semantics.
Note: You can pass structures via ref and out, thus reducing copying (it also will increase the chances of your code being inlined, as I've covered in my journal)