Re: your latest update:
This is a bug in the nullable arithmetic optimizer.
The nullable optimizer will remove the unnecessary conversion to int?
when you do something like:
short? s = null;
int? x = s + 1;
The unoptimized codegen does the equivalent of:
short? s = null;
int? x;
int? temp = s.HasValue ? new int?((int)s.Value) : new int?();
x = temp.HasValue ? new int?(x.Value + 1) : new int?();
The optimized codegen does the equivalent of:
short? s = null;
int? x;
x = s.HasValue ? new int?((int)s.Value + 1) : new int?();
However, the optimizer contains a bug; we do not remove the unnecessary conversion for equality.
Thanks for bringing it to my attention; we'll fix it for Roslyn. I'm actually about to write the nullable optimizer for Roslyn in the next couple of weeks.
UPDATE: I did write that optimizer, and if you are interested in how it works, I wrote a series of articles on it which starts here:
http://ericlippert.com/2012/12/20/nullable-micro-optimizations-part-one/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…