2009-01-05

List Find Delegate

Here is a short example of how to use the Find method together with an anonymous delegate in C#.
(there are plenty of examples on the web but most forget to use an external variable)

var myExternalVariable = "Cool";
List list = new List(new string[]    {"Cows","Are","Cool"});
var possibleWord =
    list.Find(delegate(string s)
        { return s == myExternalVariable; });

if you want to be even hairier, translate to lambda

var possibleWord = list.Find(s => s == myVariable);

Sort.

No comments: