Background
I accept this isn't something that can occur during normal code execution but I discovered it while debugging and thought it interesting to share.
I think this is caused by the JIT compiler, but would welcome any further thoughts.
I have replicated this issue targeting the 4.5 and 4.5.1 framework using VS2013:
Setup
To see this exception Common Language Runtime Exceptions
must be enabled:
DEBUG
> Exceptions...
I have distilled the cause of the issue to the following example:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication6
{
public class Program
{
static void Main()
{
var myEnum = MyEnum.Good;
var list = new List<MyData>
{
new MyData{ Id = 1, Code = "1"},
new MyData{ Id = 2, Code = "2"},
new MyData{ Id = 3, Code = "3"}
};
// Evaluates to false
if (myEnum == MyEnum.Bad) // BREAK POINT
{
/*
* A first chance exception of type 'System.NullReferenceException' occurred in ConsoleApplication6.exe
Additional information: Object reference not set to an instance of an object.
*/
var x = new MyClass();
MyData result;
//// With this line the 'System.NullReferenceException' gets thrown in the line above:
result = list.FirstOrDefault(r => r.Code == x.Code);
//// But with this line, with 'x' not referenced, the code above runs ok:
//result = list.FirstOrDefault(r => r.Code == "x.Code");
}
}
}
public enum MyEnum
{
Good,
Bad
}
public class MyClass
{
public string Code { get; set; }
}
public class MyData
{
public int Id { get; set; }
public string Code { get; set; }
}
}
To Replicate
Place a breakpoint on if (myEnum == MyEnum.Bad)
and run the code.
When the break point is hit, Set Next Statement
(Ctrl+Shift+F10) to be the opening brace of the if
statement and run until:
Next, comment out the first lamda statement and comment in the second - so the MyClass
instance isn't used.
Rerun the process (hitting the break, forcing into the if
statement and running). You'll see the code works correctly:
Finally, comment in the first lamda statement and comment out the second - so the MyClass
instance is used. Then refactor the contents of the if
statement into a new method:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication6
{
public class Program
{
static void Main()
{
var myEnum = MyEnum.Good;
var list = new List<MyData>
{
new MyData{ Id = 1, Code = "1"},
new MyData{ Id = 2, Code = "2"},
new MyData{ Id = 3, Code = "3"}
};
// Evaluates to false
if (myEnum == MyEnum.Bad) // BREAK POINT
{
MyMethod(list);
}
}
private static void MyMethod(List<MyData> list)
{
// When the code is in this method, it works fine
var x = new MyClass();
MyData result;
result = list.FirstOrDefault(r => r.Code == x.Code);
}
}
public enum MyEnum
{
Good,
Bad
}
public class MyClass
{
public string Code { get; set; }
}
public class MyData
{
public int Id { get; set; }
public string Code { get; set; }
}
}
Rerun the test and everything works correctly:
Conclusion?
My assumption is the JIT compiler has optimized out the lamda to always be null, and some further optimized code is running prior to the instance being initialized.
As I previously mentioned this could never happen in production code, but I would be interested to know what was happening.
See Question&Answers more detail:
os