|
-
May 4th, 2012, 03:39 PM
#1
Inconsistent exception behavior in FirstOrDefault() method with lambda expressiion
Greetings!
I am working with the following code in MS Visual Studio 2008:
Code:
SimpleADONetField foundField = null;
foundField = fieldList.FirstOrDefault(field => field.m_name == checkedNode.Text);
if (foundField == null)
{
SimpleADONetField tagField = (SimpleADONetField)checkedNode.Tag;
fieldList.Add(tagField);
}
The first time this code is executed, the fieldList object exists but it is empty.
When I step through this code, it behaves as I expect. Since fieldList is empty, nothing can match the condition, and foundField is set to null, which is the default value for reference types. But if I run my application through the VS debugger without stopping in this code, it throws a null reference exception, complaining that the field object is null when I try to read its m_name property.
Why do I get an exception when running the code without stopping and not get an exception when stepping through it? Which behavior is correct? I can of course check to see if the list is empty before I run this code, but I don't think I should have to do that.
Thank you very much.
RobR
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|