- Structs are the value type.
- Classes are the reference type.
- Properties of a constant instance of the struct are immutable because struct’s instance own the whole object and by declaring it as a constant means the whole object itself will be immutable.
- Properties of a constant instance of the class are mutable as class’s instance owns the object’s reference thus by declaring it as a constant only means that the further reference assignment can’t be done.
- Structs are blessed with default member-wise initializer but classes are not.
- Structs are preferable when they are small and copyable.
- With Structs, there is much less need to worry about memory leaks or multiple threads racing to access/modify a single instance of a variable.
- Since struct instances are allocated on the stack, and class instances are allocated on the heap, structs can sometimes be drastically faster (but it still depends on how many values you’re storing and the size/structure.)
- In a multi-threaded environment, for instance, with a database connection that’s opened in a different thread, structs are safer. They can be copied from one thread to another thread, without running the risk of a race condition or deadlock. Classes do not have this inherent safety unless they’re deliberately made thread-safe.
Thanks a lot for reading it.