Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
Why are there hidden copies created by the compiler when comparing C# Value Tuples with ==?

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The answer is that MSIL, the intermediate language you're decompiling back into C#, is a stack-based language, and stack-based languages have difficulty referencing what's not at the very top of the stack. So instead you can add a local variable and store results you can't easily reach.

And the reason why this seeming inefficiency isn't getting optimized out is that it simply doesn't matter. When JITed to native code, the compiler emits direct comparisons and a jump like you'd expect:

    var rng = new Random();
    var v1=(rng.Next(), rng.Next());
    var v2=(rng.Next(), rng.Next());
    
    return v1 == v2;

generates (skipping the initialization and restoring the stack in the prologue):

L004f: cmp edi, ebp
L0051: jne short L0064
L0053: cmp ebx, eax
L0055: sete al                // == && ==?
L0058: movzx eax, al          // return true && 2nd equality result
L0063: ret
L0064: xor eax, eax           // return false
L006e: ret
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!