Question: ```
foreach (var item in mainCanvas.Children)
{
    if (item is Button)
    {
        (item as Button).Content = "this is a button";
    }                
}

```

Can I use LINQ or other feature of .NET 4 to be more concise (maybe performant)?

Answer: 
You can use [**`Enumerable.OfType`**](http://msdn.microsoft.com/en-us/library/bb360913.aspx):

```
foreach (var button in mainCanvas.Children.OfType<Button>())
{
    button.Content = "this is a button";
}

```

---

**Performance Measurements**

**Method 1**: OPs original suggestion

```
foreach (var item in mainCanvas.Children)
{
    if (item is Button)
    {
        (item as Button).Content = "this is a button";
    }                
}

```

**Method 2**: OfType

```
foreach (var button in mainCanvas.Children.OfType<Button>())
{
    button.Content = "this is a button";
}

```

**Method 3**: Only cast once

```
foreach (var item in mainCanvas.Children)
{
    Button button = item as Button;
    if (button != null)
    {
        button.Content = "this is a button";
    }                
}

```

**Method 4**: for loop:

```
List<object> children = mainCanvas.Children;
for (int i = 0; i < children.Count; ++i)
{
    object item = children[i];
    if (item is Button)
    {
        (item as Button).Content = "this is a button";
    }                
}

```

**Results**

```

Iterations per second

Method 1: 18539180
Method 2:  7376857
Method 3: 19280965
Method 4: 20739241

```

**Conclusion**

* The biggest improvement can be gained by using a simple `for` loop instead of `foreach`.
* It is also possible to improve performance slightly by casting only once.
* Using `OfType` is considerably slower.

But remember to optimize readability first, and only optimize performance if you have performance profiled and found that this specific code is the performance bottleneck.
See if there is an `OfType<T>` extension.

```
foreach (var item in mainCanvas.Children.OfType<Button>()) 
{ 
    item.Content = "this is a button"; 
}

```

If not, you can use:

```
foreach (var item in mainCanvas.Children.Where(item=>item is Button).Cast<Button>()) 
{ 
    item.Content = "this is a button"; 
}

```