C# Interview Question #115

What is thread safety?

Concurrency, Performance & Design Senior Advanced

Quick Interview Answer

Thread safety means code behaves correctly when accessed concurrently by multiple threads.

Detailed Explanation

Thread safety means code behaves correctly when accessed concurrently by multiple threads.

A type can achieve thread safety through immutability, synchronization, atomic operations, thread confinement, concurrent collections, or by avoiding shared mutable state.

In ASP.NET Core, singleton services require special attention because the same instance can be used by many requests concurrently. Scoped and transient objects can also encounter concurrency when explicitly shared or used from parallel operations.