C#

C#

Made by DeepSource

Getters and setters should be mutually synchronized CS-W1078

Bug risk
Critical

Synchronization allows you to safely access resources that may be subject to race conditions. If one of the accessors, getter, for example, utilizes lock statements, it means that there is room for race condition and that the underlying resource may be concurrently accessible. In such cases, it is possible that there might be a similar race condition for the setter as well. It is therefore recommended that both accessors be mutually synchronized.

Bad Practice

public int Value
{
    get
    {
        lock (_obj)
        {
            var i = _value;
        }
    }

    // Missing `lock` statement
    set => _value = value;
}

Recommended

public int Value
{
    get
    {
        lock (_obj)
        {
            var i = _value;
        }
    }

    set
    {
        lock (_obj)
        {
            _value = value;
        }
    }
}