Feb 13 2018

Quiz: How ref returns work in C# 7.2

Test your C# 7.2 knowledge on ref returns by commenting what this program will print to the console.

Fun with Ref returns quiz in C# 7.2

Do you know what will be the output of this program? and why?

Write a comment below with your answer 

No cheating allowed 🙂

Don't forget to share the quiz with your friends and colleagues.

Permanent link to this article: https://www.productivecsharp.com/2018/02/quiz-fun-with-ref-returns-in-csharp72/

  • Initialization:
    – p1 is an independent copy of Points[0] (value 0)
    – p2 is a reference to Points[0] (value 0)
    – p3 is a reference to p1 (value 0)
    – p4 is an independent copy of p3 (value 0)
    – p5 is an independent copy of Points[0] (value 0)
    – p6 is a reference to p5 (value 0)

    Changing property values:
    – p1.X is set to 1; this also sets p3.X to 1 since it refers to the same location
    – p2.X is set to 2; this also sets Points[0].X to 2 since it refers to the same location
    – p3.X is set to 3; this also sets p1.X to 3 since it refers to the same location
    – p4.X is set to 4; this doesn’t affect anything else
    – p5.X is set to 5; this also sets p6.X to 5 since it refers to the same location
    – Points[0].X is set to 9; this also sets p2.X since it refers to the same location

    So, in the end, the output is 3 9 3 4 5 5

  • Andrew Whitworth says:

    The problem I have with ref returns, and this post really demonstrates it, is that things really start to become difficult to reason about. Sure, a good dev with experience with the feature can figure out what gets returned, but now imagine we’re passing this code off to a junior dev to fix a bug or add a new feature? As a matter of simple maintainability and reliability, I would recommend most organizations strictly avoid this feature as much as possible, just because I suspect most developers can’t answer this question and so would be setting themselves up for failure. I’m not sure if this was your intention, but your post and hardened my commitment to avoid this feature.

    • Jason Bock says:

      This feature is probably one a lot of .NET devs won’t use, primarily because they don’t need to use it. But if you want to efficiently share state that’s stored in value types (e.g. vectors, matrices), this could definitely help.

  • Jason Bock says:

    I came to the same conclusion as Thomas did, I used this weird notation to figure it out:

    P0 { 0 } in Points
    P1 { 0 }
    P2 { 0 }
    P3 { 0 }

    p1 = P1
    p2 = P0
    p3 = P1
    p4 = P2
    p5 = P3
    p6 = P3

    P0 { 9 } in Points
    P1 { 3 }
    P2 { 4 }
    P3 { 5 }

    3, 9, 3, 4, 5, 5

  • Liviu says:

    All the vars refer the same value: 9 9 9 9 9 9.

    • Thank you for your comment. The answer is incorrect. Check the first comment for a good explanation of the reasons. Remember that y default struct (value types) in C# are copied by value. A reference is copied if you use the ref keyword at the time you invoke a method (even if the signature of the method use ref return or ref readonly return)

  • >