ReSharper 2019.1 Help

Code Inspection: Inconsistent synchronization on field

This code inspection warns you about fields being used both with and without locking, thus helping you detect potential problems with thread safety. Here is an example:

public class LockingSample { private readonly List<object> _resources = new List<object>(); private readonly object _lockObject = new object(); public void AddResource(object resource) { lock (_lockObject) _resources.Add(resource); } public void RemoveResource(object resource) { // Warning: The field is sometimes used inside synchronized block // and sometimes used without synchronization if (_resources.Contains(resource)) { _resources.Remove(resource); } } }

The inspection is not triggered on const fields and in private methods that have no synchronization inside but are always used from synchronized contexts.

Last modified: 16 July 2019

See Also