Alex Rivera | Logout

C# Lambda expressions: Why should I use them?

Asked 2008-10-03T15:11:14.527
321

I have quickly read over the Microsoft Lambda Expression documentation.

This kind of example has helped me to understand better, though:

delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25

Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method?

Edit
Report

1 Answer

37

Lambda's cleaned up C# 2.0's anonymous delegate syntax...for example

Strings.Find(s => s == "hello");

Was done in C# 2.0 like this:

Strings.Find(delegate(String s) { return s == "hello"; });

Functionally, they do the exact same thing, its just a much more concise syntax.

answered 2008-10-03T15:27:56.503

Your Answer