How can I get a NullReferenceException in the following scenario?
Dim langs As IEnumerable(Of SomeCustomObject) = //some LINQ query
If langs Is Nothing Then Return Nothing
If langs.Count = 1 Then //NullReferenceException here
What am I missing here? Debug shows that langs is really just a LINQ queryresult without any results…
The exception is probably coming from the evaluation of your LINQ query. LINQ queries are evaluated in a lazy fashion: that is, no code is actually executed until you actually use the value.
For example, if you have the following (I don’t know the LINQ syntax for VB, so this is C# but the same thing applies):
Also, you will never get a
nullreturned from the creation of the LINQ query so yourIf langs Is Nothingcheck is not needed.