Stephen A. Fuqua (SAF) is a Bahá'í, software developer, and conservation and interfaith advocate in the DFW area of Texas.

Nice technique for modifying a subset of a List

March 4, 2009

One of my team members sent in the following piece of code, which is clearly intended to update the OrderNumber field for all objects in a List<T> of objects that match a particular productId. I took one look at it and thought "you can't do that!". But then I let the automated test run to see what happens... lo and behold, it worked. And well it should, once I thought about it.

list = cardQueue.FindAll(delegate(MyObject obj1)
{
    if (obj1.ProductId == processedProductId)
    {
        obj1.OrderNumber = orderNumber;
    }

    return true;
});

It had never occurred to me that you can modify an object inside of a Predicate<T> method. But now that I look at it, why not? After all, in C#, objects are passed around by reference, not value – when the method tests obj1 to see if its ProductId value is the one we are searching for, then that is the "real" object, not just a copy. Thus the real object can be modified.