开发者

Help needed using Predicate in Generics

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜