Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Created April 7, 2022 01:04
Show Gist options
  • Save siennathesane/1199ee46d24e3997387935b93fa56806 to your computer and use it in GitHub Desktop.
Save siennathesane/1199ee46d24e3997387935b93fa56806 to your computer and use it in GitHub Desktop.
Example of an incorrectly identified `InconsistentlySynchronizedField` warning in ReSharper
using System;
public class Foo {
private int[] _arr;
private readonly Guid _id;
private readonly object _lockObj;
public this[int index] {
get => _arr[index];
set => Set(index, value);
}
public Foo(Guid id) {
_id = id;
_lockObj = new object();
}
private void Set(int index, int val) {
lock (_lockObj) {
// this is throwing InconsistentlySynchronizedField, because it's
// part of a locked context, not because there is an actual
// synchronization issue.
Console.WriteLine($"Guid is {_id.ToString()}");
_arr[index] = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment