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);
精彩评论