Introduction to lambda / anonymous functions

I have this function from a plugin (from a previous post)

// This method implements the test condition for 
// finding the ResolutionInfo.
private static bool IsResolutionInfo(ImageResource res)
{
  return res.ID == (int)ResourceIDs.ResolutionInfo;
}

      

And the line calling this function:

  get
  {
    return (ResolutionInfo)m_imageResources.Find(IsResolutionInfo);
  }

      

So basically I would like to get rid of the calling function. It is called only twice (once in get and another in the set). And it might help me understand the built-in functions in C #.

+1


source to share


2 answers


get
  {
    return (ResolutionInfo)m_imageResources.Find(res => res.ID == (int)ResourceIDs.ResolutionInfo);
  }

      

Does this make it clear at all?

To further clarify the situation, looking at the reflector, it looks like this:



public T Find(Predicate<T> match)
{
    if (match == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    for (int i = 0; i < this._size; i++)
    {
        if (match(this._items[i]))
        {
            return this._items[i];
        }
    }
    return default(T);
}

      

So, as you can see, it goes through the collection, and for every item in the collection, it passes the item at that index to the predicate you passed (via your lambda). So, since we are dealing with generics, it automatically knows the type you are dealing with. This will be type T, which is the type that is in your collection. Has the meaning?

+2


source


Just to add, does the Find function on the list (which is what m_imageresources) automatically pass the parameter to the IsResoulutionInfo function?



Also, what happens first when you start or call a function?

0


source







All Articles