Find the Bug, C# edition

Thanks to Joe Roberts for showing me the code which I’ve rendered below as an exercise in bug-spotting.

The following C# defines a class with a private nullable int field _data, initialised to null, and a public int property Data, which assumes the value of _data if _data has a value, or sets _data to 3 and returns 3 otherwise.

1
2
3
4
5
6
7
8
class Foo {
  private int? _data = null;
  public int Data => _data ??= 3;
}

Foo f;
// more code here...
Console.WriteLine(f.Data);

It is very likely that this code contains a bug. What is the probable bug?

Hint, which you will certainly need

C# compiles down to Microsoft Intermediate Language (“IL”) bytecode. The example above compiles to essentially the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int32 local0;
local0 = this._data.GetValueOrDefault();
if (this._data.HasValue)
    goto label1;
local0 = 3;
this._data = new Nullable<int32>(local0);
stack.push(local0);
goto label2;

label1:
    stack.push(local0);
label2:
    return stack.pop();

Hint 2

In ROT13:

Gurer’f n enpr pbaqvgvba - jung unccraf va n zhygv-guernqrq pbagrkg?

Answer

In ROT13:

Rkrphgvba ortvaf, naq gur ybpny inevnoyr vf nffvtarq gur qrsnhyg inyhr mreb. Gur guernq vf fhfcraqrq. N arj guernq nfxf sbe gur inyhr bs Qngn, naq pbeerpgyl trgf gur inyhr guerr. Rkrphgvba bs gur bevtvany guernq erfhzrf; qngn abj unf n inyhr, fb jr whzc gb gur svefg ynory, naq erghea gur qrsnhyg inyhr bs na vag, anzryl mreb.