Array.Find with Delegate. What does it return if not found?
I have an Array<Person> myArray
and I am using the 开发者_运维问答following code
myArray.Find(o => o.name.Equals("John"));
This article in Msdn states:
Return Value
Type: T
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
If I had an Array<int>
the default value would be zero.
But, in my case I am using a class. Let's say Array<Person>
.
What would be the default for my class and how can I handle the not found case using a delegate?
The default for any reference type (class, interface, delegate) is a null reference. The default for any value type is a value where all the fields of the type are the default value for that field - so you end up with 0, \0
, false etc.
See MSDN for more details.
Assuming Person is a reference type, the default value for it would be null.
Therefore the call to Array.Find() would return null when the condition wasn't satisfied.
精彩评论