1

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 thats calling this function:

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

So basically I'd like to get rid of the calling function. It's only called twice (once in the get and the other in the set). And It could possible help me to understand inline functions in c#.

2 Answers 2

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

Does that clear it up at all?

Just to further clear things up, looking at reflector, this is what the Find method looks like:

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 loops through the collection, and for every item in the collection, it passes the item at that index to the Predicate that you passed in (through your lambda). Thus, since we're dealing with generics, it automatically knows the type you're dealing with. It'll be Type T which is whatever type that is in your collection. Makes sense?

Sign up to request clarification or add additional context in comments.

3 Comments

Thankyou, but is it okay if I did this: (ImageResource x) => x.ID == (int)ResourceIDs.ResolutionInfo ie adding a type to "x"
Don't know, try it and see? Delegate syntax allows (forces) you to provide types: new Func<ImageResource, ResolutionInfo>(delegate (ImageResource x) { return (int)ResourceIDs.ResolutionInfo; }); But yuck! The function call happens before the cast; you're just declaring a little function inline.
No, you can't add a type to the parameter, it figures it out automatically based on the type that is in the collection. I'll edit my answer to show what the Find method looks like.
0

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

Also, what happens first the cast or the function call?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.