Equality between Value Tuple types was introduced in C# 7.3. It allows code like this:
var x = (1, 2);
var y = (1, 2);
if(x == y) ...
This works fine and gives the correct result. However, the compiler introduces hidden copies of the tuple objects, and compares those instead, giving the equivalent of this:
var V_3 = x;
var V_4 = y;
if(V_3.Item1 == V_4.Item1 && V_3.Item2 == V_4.Item2) ...
Are V_3
and V_4
just defensive copies? If so, what are they defending against?
This is the smallest e.g I could come up with, but I've tried with user-defined structs, and method returns/properties as tuple members as well with similar (not always identical) output.
I'm using v5.0.202 of the .Net SDK and C# v9, but have seen this behavior since .Net Core 3.