Generic Lists (List<T>) and Predicates

posted by nblevins on Monday, June 09, 2008


So, for those of you who have much experience w/ C# / vb .net, you are most likely used to the generic list.  List&#60;T&#62;, which is a child of the System.Generic namespace, is a generic container in which you can stuff whatever items you wish, so long they are of the same type.  This includes basic structures like strings and complex objects / class instances. 

This is all pretty awesome, but the fun only increases when you are trying to check to see if one of those class instances has a value you are looking for.  This is where the Predicate Generic Delegate comes into play. In a nutshell, the Predicate "represents the method that defines a set of criteria and determines whether the specified object meets those criteria" (Thanks MSDN, that clears it right up!).  In other words, it lets you delegate an action (like the action of checking an item instance for a value) to a separate function.  This is very good for us!  Check out the following code...

Basic Class...

<code type="c#">

public class AccessItem
{
    private object _objKey;

    public object ObjKey
    {
        get { return _objKey; }
        set { _objKey = value; }
    }

    private string _strKeyName;

    public string StrKeyName
    {
        get { return _strKeyName; }
        set { _strKeyName = value; }
    }

    private string _strAccessLevel;

    public string StrAccessLevel
    {
        get { return _strAccessLevel; }
        set { _strAccessLevel = value; }
    }

    public AccessItem(object Key, string KeyName, string AccessLevel)
    {
        _objKey = Key;
        _strAccessLevel = AccessLevel;
        _strKeyName = KeyName;
    }
}

</code>

Create a List and add some class instances...

<code type="c#">

List<AccessItem> tempList = new List<AccessItem>();

for(int x = 0; x < 10; x++)
{
  tempList.Add(new AccessItem(x, x + "Name", x + "AccessLevel");
}

</code>

Now, lets say that you want to find the access item w/ the key of 5.  If this were a basic int list, you could simply use the Contains bool in the List&#60;T&#62; properties.  In this case (since we are not dealing w/ a basic object), we are either stuck w/ looping through the list and checking values of each instance, or using the predicate.  The syntax for checking w/ a predicate is as follows...

<code type="c#">

tempList.Exists(delegate(AccessItem ai) { return ai.ObjKey == 5; })

</code>

Anyway, it is very painless and very maintainable.  Also, you have full access to your stuffed object, so feel free to drill down as far as you would wish in order to compare values...  Very cool!



Comments

Generic Lists (List) and Predicates
Friday, February 12, 2010
The Predicate has been defined well "represents the method that defines a set of criteria and determines whether the specified object meets those criteria"
mhg@,a href="http://www.nextpimp.com/">free ringtones</a>
Comment By: freeringtones

Name:
Email (not shown):
Home Page (optional):
Title:

Sorry, I don't support html entry... ;)