Search code examples
c#multithreadingarraysthread-safetycritical-section

Thread safety in C# arrays


Does having 2 different threads :

  • one reading from a C# array (e.g from first location),
  • and another one writing to the same C# array but to a different location(e.g to the last location)

is thread safe or not?
(And I mean here without locking reading nor writing)


Solution

  • This particular case is safe, yes.

    Reading and writing to different parts of an array does not interfere with the other operations.

    However, reading and writing to the same location can give you problems, depending on the type of element, and the size of the elements.