I’m doing course 3354 (Implementing System Types and Interfaces in the .NET Framework 2.0) and it is said that for simple classes, with members variables and functions, it is better to use a struct than a class because of overhead.
I have never heard of such a thing, what is the validity of this claim?
I recommend to never use a struct unless you have a very specific use-case in mind and know exactly how the struct will benefit the system.
While C# structs do allow members, they work a good bit different then classes (can’t be subtyped, no virtual dispatch, may live entirely on the stack) and the behavior changes depending upon lifting, etc. (Lifting is the process of promoting a value type to the heap — surprise!)
So, to answer the question: I think one of the biggest misnomers in C# is using structs “for performance”. The reason for this is ‘overhead’ can’t be truly measured without seeing how it interacts with the rest of the system and the role, if anything of note, it plays. This requires profiling and can’t be summed up with such a trivial statement as “less overhead”.
There are some good cases for struct value types — one example is a composite RGB value stored in an array for an image. This is because the RGB type is small, there can be very many in an image, value types can be packed well in arrays, and may help to keep better memory locality, etc.