tags:

views:

370

answers:

5

I am curious on how the following code works, especially the part the contains the delegate stuff since I am new to it. Also, are there any bottlenecks in the code, for example, would using a SortedDictionary be better than using a List and then sorting it using LINQ? Ok, here is the code:

public class ColorManager
{
    private List<ColorData> colorCodes = new List<ColorData>();

    public List<ColorData> ColorCodes
    {
        get
        {
            var sortList = from a in this.colorCodes
                           orderby a.colorCode ascending
                           select a;

            return sortList.ToList();
        }
    }

    public void FillData(DataTable table)
    {
        for(int row = 0; row < table.Rows.Count; row++)
        {
            ColorData cData = new ColorData();
            string name = table.Rows[row]["Color"].ToString().Trim();

            if(!this.colorCodes.Exists(
                delegate(ColorData e)
                {
                    return e.ColorCode == name;
                }))
            {
                cData.Add(table.Rows[row]["Color"].ToString());
                this.colorCodes.Add(cData);
            }
            else
            {
                this.colorCodes.Find(
                    delegate(ColorData e)
                    {
                        return e.ColorCode == name;
                    }).Count++;
            }
        } 
    }
}
A: 

Its an anonymous delegate that serves as the selection criteria for the find expression. In words, if the color is equal to the color given by name, it will return true and Find will add this to the IEnumerable that it is generating. If it returns false, Find will not contain it. It can be written more succinctly as

p => p.ColorCode == name
Steve
A: 

Exists and Find are generic extension methods. They for for each loop over each item in the collection. Passing the delegate a parameter (defined like "(ColorData e)"), and returning a boolean.

chilltemp
A: 

Delegates:

The delegates in this example are implementing a Func method. Basically, what you're doing, is creating a method on the fly that takes a ColorData argument, and returns a bool.

This lets you do your Exists() and Find() calls on every member of your IEnumerable collection for checking and filtering.

As for Sorted Dictionary:

If this is the only place you're using it, you'd be better off using SortedDictionary, since you wouldn't be resorting it constantly. Right now, every time you call ColorCodes you're resorting. A SortedDictionary would be constantly sorted.

Reed Copsey
+2  A: 

I am curious on how the following code works, especially the part the contains the delegate stuff since I am new to it.

First take a look at this ColorCodes property accessor:

        var sortList = from a in this.colorCodes
                       orderby a.colorCode ascending
                       select a;

It returns a list of all the color codes (this.colorCodes) in ascending order (...orderby a.colorCode ascending). Simple so far. How about the meatier FillData method?

First, we'll loop over every row in this table:

    for(int row = 0; row < table.Rows.Count; row++)
    {

Then, we're going to look at the Color column of the current row.

        ColorData cData = new ColorData();
        string name = table.Rows[row]["Color"].ToString().Trim();

If that column has no match in our list of color codes, this if condition is true. The Exists method takes a single-argument function that returns a bool, which is what this anonymous delegate is: a single-argument (ColorData e) function that returns a boolean (e.ColorCode == name). It gets negated (!this.[...]), so if a match is found, the condition is false.

        if(!this.colorCodes.Exists(
            delegate(ColorData e)
            {
                return e.ColorCode == name;
            }))
        {
            cData.Add(table.Rows[row]["Color"].ToString());
            this.colorCodes.Add(cData);
        }

Otherwise, if there's a match, find the name using another anonymous delegate, and add one to the count of items of that color.

        else
        {
            this.colorCodes.Find(
                delegate(ColorData e)
                {
                    return e.ColorCode == name;
                }).Count++;
        }
    }

Note that this code is somewhat inefficient, since the two different anonymous delegates really do the same thing and could be shared.

John Feminella
How could it be refactored so they would be sharing?
Xaisoft
You can use an anonymous method anywhere a delegate is expected. Try var matchThisColor = new Func<ColorCode, bool>(e => e.ColorCode == name);.
John Feminella
+2  A: 

Actually, there are more delegates there than you expect. The LINQ query:

        var sortList = from a in this.colorCodes
                       orderby a.colorCode ascending
                       select a;

is actually:

var sortList = this.colorCodes.OrderBy(a => a.colorCode);

which (for LINQ-to-Objects) is the same as:

var sortList = this.colorCodes.OrderBy(delegate (ColorData a) {
  return a.colorCode;
})

This uses a delegate to identify the item to sort by - i.e. "given a ColorData, I'll give you the colorCode". Note that many select uses also involve a delegate, but not in this case (the trivial select a is removed by the compiler).

In the code:

        if(!this.colorCodes.Exists(
            delegate(ColorData e)
            {
                return e.ColorCode == name;
            }))

we are using a predicate; "given a ColorData, I'll tell you whether it is a match, by testing the name for equality". Note that name here is "captured" into the delegate, which involves some fairly complex compiler tricks (I won't describe them).


Re efficiencies; it is hard to be sure, but perhaps:

    Dictionary<string, ColorData> items = new Dictionary<string, ColorData>();
    foreach(DataRow row in table.Rows) {
        string name = row["Color"].ToString().Trim();
        ColorData cData;
        if (items.TryGetValue(name, out cData)) {
            cData.Count++;
        } else {
            cData.Add(name);
            colorCodes.Add(cData);
            items.Add(name, cData);
        }
    }

This avoids lots of duplication, and uses a dictionary to find values rather than constantly using Contains/Find. In many cases, a LINQ GroupBy might have helped, but not in this case, perhaps.

Marc Gravell