First off, according to http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx , the List.Find method is only listed as throwing ArgumentNullException. However I have the following test code which, when using Find with an anonymous delegate, throws a NullReferenceException when the object being searched for is not found.
namespace MyTestNS
{
class MyTestClass
{
[TestMethod()]
public void ArrayMatchTest()
{
List<A> objArray = new List<A>();
objArray.Add(new A("1","one"));
objArray.Add(new A("2", "two"));
string findStr = "3";
string foundVal;
// Find using an anonymous delegate:
foundVal = objArray.Find(delegate(A a) // <- System.NullReferenceException: Object reference not set to an instance of an object..
{
if (a.name == findStr)
return true;
else return false;
}).value;
}
}
}
I don't understand why I'm getting a NullReferenceException instead of the Find just not finding the item and returning null. I'm 90% sure it's some subtle coding error on my part that I just haven't seen, but this has been bugging me all day, please help!
EDIT: I should mention I inherited this convoluted code form someone else, so the twisty code you see above is a somewhat simplified version of whats failing in my real code.
Find
is returning null. But then you are dereferencing that result. That is, you're invoking:
Find(...).value
You have a lot of stuff going on in one place - I would suggest that you simplify it a bit so that you can see exactly what is going wrong.
A simpler version of your Find
invocation is this:
A a1 = objArray.Find(a => a.name == findStr);
What happens if your Predicate<A>
returns false
? The objArray
returns you a null
reference and a
is assigned to it. Now it is clear to see that using a
will cause a NullReferenceException
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.