Wednesday, September 10, 2008

Lambda expressions with multiple parameters

After writing the calling code for the heap I realised that I could have used a Lambda Expression:

var heap3 = new Heap<char>(first, second => second.CompareTo(first));

but that does not work as it the compiler complains about "The name 'first' does not exist in the current context". The solution to this is to wrap it with brackets:
var heap3 = new Heap<char>((first, second) => second.CompareTo(first));
foreach (char c in data)
heap3.Push(c);
while (heap3.Count != 0)
Console.WriteLine(heap3.Pop());

No comments: