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.