Code inspection: Value assigned to a property of non-variable qualifier expression can be lost
This inspection reports assignments and ++ or -- operations that may modify a temporary copy of a struct instead of the original value.
It is reported for cases where the code is valid, but the accessed value might still be a struct copy at runtime. A common case is an unconstrained generic type parameter used through a readonly field or another immutable access path. If the type argument turns out to be a struct, the member write can be lost.
Example
public interface IHasCount
{
int Count { get; set; }
}
public class Example<T>
where T : IHasCount
{
private readonly T value;
public void Set()
{
value.Count = 1;
}
}
public interface IHasCount
{
int Count { get; set; }
}
public class Example<T>
where T : class, IHasCount
{
private readonly T value;
public void Set()
{
value.Count = 1;
}
}
When the accessed value is a known struct copy, a related quick-fix can rewrite the code to copy and update the struct explicitly with a with expression:
Example with struct copy
public class C
{
public MyStruct? Value { get; set; }
public void M()
{
Value.Value.Count = 1;
}
}
public struct MyStruct
{
public int Count;
}
public class C
{
public MyStruct? Value { get; set; }
public void M()
{
Value = Value.Value with { Count = 1 };
}
}
public struct MyStruct
{
public int Count;
}
14 April 2026