which are the best ways to loop through and check through lists?

I found this on MSDN and some other blogs and articles:

void CheckListForThing()
        {
            for (int i = 0; i < ThingList.Count; i++)
            {
              //this part is strange to me, I can barely understand it, but for what it does it does exactly what I want
                ObjectOnList = ThingList.Find(
                    delegate(ObjectOnListType _objectOnList)
                    {

                        return _objectOnList.VariableToCheck == i;
                    }
                );
                if (ObjectOnList != null) //check to see if an object on the list matches the variable compared to i
                {
                      //do something


                }


            }

        }

Id like to see how else I should use this to find stuff on lists, other ways to test for values.

also could someone explain to me what this is exactly

Find(
                    delegate(ObjectOnListType _objectOnList)
                    {

                        return _objectOnList.VariableToCheck == i;
                    }
                );

what is a delegate, I cant make sense of what it is. Its like a temporary function command right?

how come you can do awesome things like “return a CheckedValue that is equal to Whatever”. Seems really useful. Can I do that other places besides in delegate?

A delegate is reference to a function with a specific signature ( return type + parameter types)

The Find method expects a function that takes as parameter an element of the list and returns true or false. True means there is a match, and false means no match. The function is applied on each element of the list and Find will return the first element that matches.

The neat thing about this is you can define your function however you want so that you can define your very own search criteria. This is only one use case of delegates.

Official documentation of the Find method here:

1 Like

thank you I’ll check it out.