A software engineers guide to the galaxy

Sep 19, 2008

strange c# lambda expression behavior

Here's a nice one:

public static void Foreach<T>(
this IEnumerable<T> collection,
Action<T> action)
{
foreach (var item in collection)
action(item);
}

public static IEnumerable<T> Foreach<T>(
this IEnumerable collection,
Func<Object, T> action)
{
foreach (var item in collection)
yield return action(item);
}

var files = Directory.GetFiles(dir, "*.xml");
// #1
files.Foreach(xmlFile => DoSomething(xmlFile));

// #2
files.Foreach(xmlFile => { DoSomething(xmlFile); });

Given that the files array is not empty.

#1 doesn't call DoSomething(), #2 does. Why?

Labels: ,

Aug 28, 2008

Memory leaks in .NET

I'm currently working on creating an ESB solution (like Biz Talk). In the process a memory leak was discovered, I found this site which describes how to use Perfmon to track leaks in .Net code.

After alt of debugging we found that the MSMQ integration in .NET leaks memory when fetching public Queues :(

Labels: , ,