Wednesday, 10 February 2010

Lambada as Predicate method, c#3.5

{
    List<string> ZoneList = new List();
.
.
.
    for (int i = 0; i > 10; i++)

    {
       ZoneList.Add(i.ToString());
    }
  
.
.
.
    if (ZoneList.Exists(listItem => listItem != notExistedWord))

    {
       ZoneList.Add(notExistedWord);
    }
}

ZoneList.Exists(Predicate<string> match) is a List method that needs predicate-method as input paramter, the Predicate-method must has a string input paramter and Boolean output parameter, the string input paramter will be used to carry one by one item of the List items for being proccessed inside predicate-method, alternativly, we can use lambada expression listItem => listItem==searchedWord, where left side of => is concidered input for the right side, this expression can be assigned to delegate just like that:

delegate Boolean SearchDelegate(string listItem);
SearchDelegate exists = listItem => listItem==searchedWord

searchedWord is a variable that must be explicitly defined before lambada is defined, searchedWord will be stored for use even if it goes out of scope, this is one rule of many lambada-variable-scope rules.

No comments:

Post a Comment