开发者

Array.Find with Delegate. What does it return if not found?

开发者 https://www.devze.com 2023-03-24 08:29 出处:网络
I have an Array<Person> myArray and I am using the 开发者_运维问答following code myArray.Find(o => o.name.Equals(\"John\"));

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消