Memory Model

This lesson discusses C#'s memory model.

We'll cover the following

Memory Model

A memory model is defined as the set of rules according to which the compiler, the processor or the runtime is permitted to reorder memory operations. Re-ordering operations allows compilers and the like to apply optimizations that can result in better performance. However, this freedom can wreak havoc in a multithreaded program when the memory model is not well-understood with unexpected program outcomes. The C# memory model permits reordering of memory operations in a method, as long as the behavior of single-threaded execution doesn’t change.

Consider the code snippet below. Take a moment to read the code and think about the outcome of running the program.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.