ReSharper 2017.2 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 smart enough not to warn you about usages in private methods that has no synchronization inside but are always used from synchronized contexts.

Last modified: 12 October 2017

See Also