An example that uses List.Exists(Predicate match)


Over an internal alias someone asked for an example of List.Exists(Predicate match) … I thought you might find it fun and google might help me find it later when I need it 😉

 

List<string> l = new List<string>();

l.Add(“Red”);

l.Add(“Green”);

l.Add(“Blue”);

l.Add(“Purple”);

 

//prints “true” as “Red” is in the list

Console.WriteLine(l.Exists(delegate(string s) { return s == “Red”; }));

 

//prnts “false” as “Pink” is not in the list

Console.WriteLine(l.Exists(delegate(string s) { return s == “Pink”; }));

Comments (6)

  1. Dr. Search says:

    You really should start using Lookout.:-)

  2. Equivalent FindAll and ConvertAll examples:

    http://weblogs.asp.net/justin_rogers/archive/2004/04/30/123780.aspx

    A ForEach sample as well:

    http://weblogs.asp.net/justin_rogers/archive/2004/04/30/123807.aspx

    Exists is one of those completeness APIs. Not used very often, but needs to be there, even if just for basic testing and checking over a collection.

  3. Ryan Cromwell says:

    Great example… I’m sure that’s going to be a google legacy…

  4. Dr. Search — I *do* use LookOut and I love it, but i don’t save mail forever and other folks can’t find in trapped in my PST 😉

  5. Google Legacy –

    Search = "Why do I Exist"

    Results = #1 Brad Abrams – An eample that uses List<T>.Exists…

    Yep, that’ll be a google legacy alright 😉