Array.Find<> example

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was browsing the vs2005 beta and was wondering if someone could show
me an example of the Array.Find<> usage, specifically how to write
"Predicates".

Say I have an array of DateTime objects, and I wanted to find the date
Apr. 23, 2005 8:30am from the array - how does one write the
"predicate", and, does the "search" terminate after it finds the value,
or are all values always scanned?


Is there any sources available yet that discuss this area of C#
generics?


TIA
 
I was browsing the vs2005 beta and was wondering if someone could show
me an example of the Array.Find<> usage, specifically how to write
"Predicates".

You can write them as regular or anonymous methods, as long as they
Say I have an array of DateTime objects, and I wanted to find the date
Apr. 23, 2005 8:30am from the array - how does one write the
"predicate"

Something like

DateTime dt = Array.Find<DateTime>( yourDateTimeArray,
delegate(DateTime obj) {
return obj == new DateTime(2005, 4, 23, 8, 30, 0);
}
);

This however doesn't make sense. Since you know the value of the item
you're searching for, you could just as well assign it to dt directly.
You would typically use Find for searching with a criteria other than
equality.

does the "search" terminate after it finds the value,
or are all values always scanned?

Yes, it returns the first match found.




Mattias
 
Back
Top