Wednesday, January 07, 2009

public T Find (Predicate <T> match)

Like Delegates and Events in c# - at first - predicates and dynamic functions, Find and Exists on Generic Collections, confuse the !@#$ out of me everytime I need to use them.

I'm going to write some stuff down here that gets me over the line - it makes some sense to me, so that I can refer back to it. This post will probably change as I "grow wiser", but for now, it is here to stop me having to "re-aquaint myself" with this topic, via useless MSDN help, and examples about dinosaurs, everytime I strike it.

I want to find an occurence of an object, in my Generic collection.

So, you use, MyList.Find. All the doco words it like this:

public T Find (Predicate match)

Now this is just the "signature" of the Find method you are about to use.

Find, is a method on the List class.  

Now T was the first point of confusion. But as best I can tell, this is just a naming convention. It means your particular Type of object. (Your class name.) So it's kind of obvious - the Find method on your Generic collection, returns to you, one of your own objects. (Naturally).

From there, the next point of confusion is the word Predicate.  Any concept I'd managed to grasp until that point evaporated when I read that word. It just wouldn't gel.

After the 20th time I'd hacked a solution in to find an object in my generic collection, I decided to look up the word predicate in a dictionary.

It basically talks about "claiming, asserting, affirming something about the subject..."

I guess the "subject" in our case, is our function that is going to define what consitutes a match so we are asserting that the subject of that function, is one of our custom objects...a "T" within our list of T's  - List.

So, Find, on the List class, is going to loop through all its members, (which are, remember your very own objects) and return you one of them, which, when passed to the method you tell it to call ("match"), returns true.

If this is way to over simplified for you and that annoys you, move on to some other site or Google search result, if it doesn't annoy you and you can further enlighten me, leave me a comment that sets me straight, if it helps you too, then great! Good luck.

Time will tell whether this helps me again or not...