15
  1. Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant?

  2. How these 3 are different for each other?

  3. Was one replaceable for another?

I didn't get any concrete answer when i did Googling

Mark Canlas
  • 9,385
  • 5
  • 41
  • 63
Gopi
  • 5,656
  • 22
  • 80
  • 146
  • 5
    there are 4 things in that list... – Sam Holder Apr 16 '10 at 10:49
  • @Sri Kumar: I didn't down vote but I would guess it is a mix between the fact that this is readily available information and that your English is not perfect. –  Apr 16 '10 at 16:53
  • 3
    Sometimes it IS readily available, however SO in it's very nature is supposed to be a repository of information so that it's users don't always have to go elsewhere. Jeff Atwood said himself an answer that is simply a link is NOT helpful, but a copy and paste of information FROM that link IS helpful. – Chase Florell Oct 28 '10 at 02:44

1 Answers1

32

LINQ is a broad technology name covering a large chunk of .NET 3.5 and the C# 3.0 changes; "query in the language" and tons more.

A delegate is comparable to a function-pointer; a "method handle" as an object, if you like, i.e.

Func<int,int,int> add = (a,b) => a+b;

is a way of writing a delegate that I can then call. Delegates also underpin eventing and other callback approaches.

Anonymous methods are the 2.0 short-hand for creating delegate instances, for example:

someObj.SomeEvent += delegate {
    DoSomething();
};

they also introduced full closures into the language via "captured variables" (not shown above). C# 3.0 introduces lambdas, which can produce the same as anonymous methods:

someObj.SomeEvent += (s,a) => DoSomething();

but which can also be compiled into expression trees for full LINQ against (for example) a database. You can't run a delegate against SQL Server, for example! but:

IQueryable<MyData> source = ...
var filtered = source.Where(row => row.Name == "fred");

can be translated into SQL, as it is compiled into an expression tree (System.Linq.Expression).

So:

  • an anonymous method can be used to create a delegate
  • a lambda might be the same as an anon-method, but not necessarily
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900