<p>I'm writing code like this, doing a little quick and dirty timing:</p>

<pre><code>var sw = new Stopwatch();
sw.Start();
for (int i = 0; i &lt; 1000; i++)
{
    b = DoStuff(s);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
</code></pre>

<p>Surely there's a way to call this bit of timing code as a fancy-schmancy .NET 3.0 lambda rather than (God forbid) cutting and pasting it a few times and replacing the <strong><code>DoStuff(s)</code></strong> with <strong><code>DoSomethingElse(s)</code></strong>?</p>

<p>I know it can be done as a <code>Delegate</code> but I'm wondering about the lambda way.</p>

You could try writing an extension method for whatever class you're using (or any base class).
I would have the call look like:
```
```
Stopwatch sw = MyObject.TimedFor(1000, () => DoStuff(s));
```
```
Then the extension method:
```
```
public static Stopwatch TimedFor(this DependencyObject source, Int32 loops, Action action)
{
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < loops; ++i)
{
    action.Invoke();
}
sw.Stop();

return sw;
}
```
```
Any object deriving from DependencyObject can now call TimedFor(..). The function can easily be adjusted to provide return values via ref params.
--
If you didn't want the functionality to be tied to any class / object you could do something like:
```
```
public class Timing
{
  public static Stopwatch TimedFor(Action action, Int32 loops)
  {
    var sw = new Stopwatch();
    sw.Start();
    for (int i = 0; i < loops; ++i)
    {
      action.Invoke();
    }
    sw.Stop();

    return sw;
  }
}
```
```
Then you could use it like:
```
```
Stopwatch sw = Timing.TimedFor(() => DoStuff(s), 1000);
```
```
Failing that, this answer looks like it has some decent "generic" ability:
Wrapping StopWatch timing with a delegate or lambda?