Code inspection: Change lock field type to 'System.Threading.Lock'
Starting from C# 13, in lock
statements it is suggested to use a dedicated object instance of the System.Threading.Lock
type for the best performance and readability.
This inspection identifies other types used in lock
statements and suggests replacing the type of the corresponding field to System.Threading.Lock
.
public class Sample
{
readonly List<int> _list = [];
readonly object _lock = new();
public void Add(int k)
{
lock (_lock)
{
_list.Add(k);
}
}
public int Get(int index)
{
lock (_lock)
{
return _list[index];
}
}
}
public class Sample
{
readonly List<int> _list = [];
readonly Lock _lock = new();
public void Add(int k)
{
lock (_lock)
{
_list.Add(k);
}
}
public int Get(int index)
{
lock (_lock)
{
return _list[index];
}
}
}
Last modified: 08 November 2024