"=>" can read "goes" ( source: Eric Lippert ) and just separates the argument from the operation in the lambda expression. In this case, the lambda is full. Best examples:
var subList = list.FindAll(item => item.Type == "Foo");
(find all elements where element type is Foo)
etc .. In C # 2.0 this can also be written:
var subList = list.FindAll(delegate(SomeType item) { return item.Type == "Foo";});
And this is a quick way to express the "inline" function, as well as support for "closing", i.e. It can also be:
string itemType = ... var subList = list.FindAll(item => item.Type == itemType);
For this, otherwise, type-definiton is required to be passed in the element type:
class Searcher { public string itemType; public bool Find(SomeType item) {return item.Type == itemType;} } ... Searcher searcher = new Searcher(); searcher.itemType = ... var subList = list.FindAll(searcher.Find);
In fact, this is exactly what the compiler does for us (both for "delegation" and for lambda use). The biggest difference is that lambda can also express Expression , for example for LINQ.
Marc gravell
source share