- 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.
Problem-solving requires two types of mental skills, analytical and creative. I know this sounds very high level So let’s talk about the basic fundamentals what generally I try to follow in day to day professional and personal activities.
- Listen carefully You need to pay very close attention to any information in the problem description. The optimal solution for a sorted vs unsorted array will be totally different so don’t miss anything.
- Brute Force Yes correct, Get the brute force solution as soon as possible, don’t think about the optimal solution. I always catch the first thought that comes to my mind. Don’t think about code, the algorithm just rough idea.
- Optimize Now it’s time to work on your Brute Force with some BUD (Bottlenecks, Unnecessary work and last Duplicated work) techniques.
- Remove the unused information.
- Solve it manually on paper with an example.
- Reverse engineer your thought process. HOW DID YOU DO?
- Make a Time VS Space trade-off. CAN YOU REDUCE-IT MORE?
- Implement Congratulations now you have an optimal solution on paper. The last goal is to write beautiful, modularize code.
If you are trying to solve the problem in interview KEEP TALKING because your interviewer wants to hear how you’re going to approach the problem and it will make the interview more interactive.
Thanks a lot for reading the quick-notes!