Question: Suppose the following code:

```
foreach(Item i on ItemCollection)
{
   Something s = new Something();
   s.EventX += delegate { ProcessItem(i); };
   SomethingCollection.Add(s);
}

```

Of course, this is wrong because all the delegates points to the same Item. The alternative is:

```
foreach(Item i on ItemCollection)
{
   Item tmpItem = i;
   Something s = new Something();
   s.EventX += delegate { ProcessItem(tmpItem); };
   SomethingCollection.Add(s);
}

```

In this case all the delegates point to their own Item.

What about this approach? There is any other better solution?

Answer: 
If ItemCollection is a [(generic) List](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) you can use its [ForEach](http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx)-method. It will give you a fresh scope per i:

```
ItemCollection.ForEach(
    i =>
    {
        Something s = new Something();
        s.EventX += delegate { ProcessItem(i); };
        SomethingCollection.Add(s);
    });

```

Or you can use any other suitable [Linq](http://msdn.microsoft.com/en-us/netframework/aa904594.aspx)-method - like [Select](http://msdn.microsoft.com/en-us/library/bb548891.aspx):

```
var somethings = ItemCollection.Select(
        i =>
        {
            Something s = new Something();
            s.EventX += delegate { ProcessItem(i); };
            return s;
        });
foreach(Something s in somethings)
    SomethingCollection.Add(s);

```
The problem you are facing here is related to such language construct as **closure**.
Second piece of code fixes the problem.