0

Thread Safty for Static Class & it's members : - Are all members in static class thread safe ?

SO19
  • 105
  • 9

1 Answers1

1

Static constructors are thread safe - they are guaranteed to be run only once per application domain, before any instances of a class are created or any static members are accessed.

Other static methods aren't thread safe as per se, CLR doesn't distinguish between static and instance methods in this aspect.

However, if a method doesn't access any data which can be visible to other threads or publish such data it can be considered thread safe. It applies to both - static and instance methods. Otherwise you need to add explicit synchronization to make access to a shared data thread safe.

Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44