Taking the following code in VB2012, I expect foo to be initialized to Nothing:
Dim foo As Func(Of Integer) = If(True, Nothing, Function() 0)
However, it throws an ArgumentException:
Delegate to an instance method cannot have null 'this'.
I donβt quite understand this error message, but the situation becomes intimidating if I change the type of foo to Func (Of Integer, Integer). In this case, the code works without errors, but foo becomes a mysterious lambda expression that throws a NullReferenceException when called.
If I use the traditional if statement instead of the If function, the code works as expected.
Can someone explain this behavior to me?
source
share