开发者

Help needed using Predicate in Generics

开发者 https://www.devze.com 2023-03-09 14:18 出处:网络
Presently i use a method which returns me the ICommand object based on the string comparisons got from the supplied key.

Presently i use a method which returns me the ICommand object based on the string comparisons got from the supplied key.

public ICommand getCommand(string mCommand)
        {
            foreach (object obj in objCommandList)
            {
                ICommand command = (ICommand)obj;
                if (command.m_strCommandName == mCommand)
                {
                    return command;
                }
            }
        return null;

        }

where objCommandList contains ICommand objects.

Now I want to improve my code or rather try an alternative to search amongst the collection i.e using an option such开发者_如何学Python as Predicate delegate in retrieving the filtered object amongst the collection.

ie.

objCommandList.Find(Predicate syntax which is needed here...)

Can anyone help me with this.


You might try something like this:

objCommandList.Find(delegate(Icommand command) { return command.m_strCommandName == mCommand; });

or

objCommandList.Find(c => c.m_strCommandName == mCommand);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号