ReSharper 2024.1 Help

Code Inspection: Inconsistent synchronization on field

Category

Potential Code Quality Issues

ID

InconsistentlySynchronizedField

EditorConfig

resharper_inconsistently_synchronized_field_highlighting

Default severity

Warning

Language

C#

Requires SWA

No

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: 15 April 2024