.NET Tip of The Day
Learn one new .NET trick every day
Login or Join
.NET Tips & Tricks Community RSS

Those who prefer reading this site through RSS now can also subscribe for updates to .NET Tips & Tricks Community.

Create elegant code with generic collections and Predicate delegate

Let's review the following code which works with generic collection:

    List<String> urls = new List<String>();    //represents a bunch of urls

    ...

 

    //Remove all urls that don't start with "http://"

    for (int i = urls.Count - 1; i >= 0; i--)

    {

        if (!urls[i].StartsWith("http://", StringComparison.OrdinalIgnoreCase))

        {

            urls.RemoveAt(i);

        }

    }

This works well but we can do a little better using Predicate (Predicate is essentially a delegate that allows us to filter based on a certain criteria). First, we have to define the method to be the predicate to validate that an url don't starts with an "http://". This would be:

    bool IsNonHttp(string url)

    {

        return !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase);

    }

Which is essentially the same as the statement we had above. Separating it into a separate method has a few advantages, practically the ability to reuse the same logic for other uses. Now once we have this predicate removing all urls that don't start with an "http://" would be:

    urls.RemoveAll(IsNonHttp);

You can also write the whole thing in one line if you so choose by using an anonymous delegate this way:

    urls.RemoveAll(delegate(string url) {

        return !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase);

    });

Other methods of List<T> which take a Predicate:

  • List.Exists - determines whether the List contains elements that match the conditions defined by the specified predicate.
  • List.Find - searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List.
  • List.FindAll - retrieves the all the elements that match the conditions defined by the specified predicate.
  • List.FindIndex - searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within the List or a portion of it.
  • List.FindLast - searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List.
  • List.FindLastIndex - searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within the List or a portion of it.
  • List.TrueForAll - determines whether every element in the List matches the conditions defined by the specified predicate.

9/22/2007
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.