Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates and Lambdas and LINQ, Oh My!

As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long.

I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this project) among others. Everything I've read to this point suggests that to utilize LINQ I'll need to fully understand the following (Delegates, Anonymous Methods and Lambda Expressions).

OK, so now comes the fun. I've CONSUMED delegates in the past as I have worked with the .NET event model, but the majority of the details have been hidden from me (thanks Microsoft!). I understand that on a basic level, delegate instances are pointers to methods (a gross over-simplification, I know).

I understand that an anonymous method is essentially an in-line unnamed method generally (if not exclusively) created as a target for a delegate.

I also understand that lambdas are used in varying ways to simplfy syntax and can be used to point a simple anonymous method to a delegate.

Pardon me if my any of my descriptions are WAY off here, this is the basic level to which I understand these topics.

So, the challenge:

  1. Can anyone tell me if at least on a basic level if my understanding of these items is even close? I'm not looking for complex esoteric minutiae, just the basics (for now).

  2. To what degree do I need to truly understand these concepts before applying LINQ in a project to reasonable effect? I want to understand it fully and am willing to spend the time. I simply may not HAVE the time to fully grok all of this stuff before I need to produce some work.

  3. Can anyone point me to some good articles that explain these subjects and apply them to "real world" examples so that I can get my head around the basics of the topics and application of them? What I mean by real world, is how might I use this in the context of "Customers and Invoices" rather than abstract "Vectors and Shapes" or "Animals and Cows". The scenario can be somewhat contrived for demonstration purposes, but hopefully not strictly academic. I have found a number of examples on-line and in books, but few seem to be "Plain English" explanations.

Thank you all in advance for your patience, time and expertise.

like image 938
Steve Brouillard Avatar asked Jan 13 '09 16:01

Steve Brouillard


People also ask

What is the difference between lambdas and delegates?

They are actually two very different things. "Delegate" is actually the name for a variable that holds a reference to a method or a lambda, and a lambda is a method without a permanent name. Lambdas are very much like other methods, except for a couple subtle differences.

What is lambda and LINQ?

The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.

What is the difference between LINQ and lambda?

Language Integrated Query (LINQ) is feature of Visual Studio that gives you the capabilities yo query on the language syntax of C#, so you will get SQL kind of queries. And Lambda expression is an anonymous function and is more of a like delegate type.

What is delegate in LINQ?

A delegate is a type that represents references to methods with a particular parameter list and return type. Delegates allow methods to be passed as parameters. Delegate is a reference type and it holds the reference of a method. All delegates implicitly derive from System. Delegate class.


3 Answers

Where can i find a good in depth guide to C# 3?

1) Your knowledge so far seems ok. Lambda expressions are turned into anonymous methods or System.Linq.Expressions.Expression's, depending on context. Since you aren't using a database technology, you don't need to understand expressions (all lambdas will be anonymous methods). You didn't list Extension methods, but those are very important (and easy) to understand. Make sure you see how to apply an extension method to an interface - as all the functionality in linq comes from System.Linq.Enumerable - a collection of extention methods against IEnumerable(Of T).

2) You don't need a deep understanding of lambdas.

The arrow syntax ( => ) was the biggest hurdle for me. The arrow separates the signature and the body of the lambda expression.

Always remember : Linq methods are not executed until enumerated.

Watch out for using loop variables in a lambda. This is a side effect from deferred execution that is particularly tricky to track down.

3) Sure, Here are some of my answers that show linq method calls - some with xml.

  • List splitting
  • Simple Xml existence search
  • Xml projection - shape change
like image 148
Amy B Avatar answered Oct 29 '22 00:10

Amy B


1) Those descriptions sound pretty accurate to me. Sometimes anonymous methods and lambda expressions will need to create a new type to put the target of the delegate in, so they can act as closures.

2/3) I would read up a bit until you're happy with delegates, anonymous methods and lambda expressions. I dedicate a chapter to the delegate-related changes in each of C# 2.0 and C# 3.0 in C# in Depth, although of course other books go into detail too. I have an article as well, if that helps.

As for examples - delegates are used for many different purposes. They're all different ways of looking at the same functionality, but they can feel very different:

  • Providing the code to call when you start a new thread
  • Reacting to UI events
  • Providing the filter, selection, ordering etc for a LINQ query
  • Providing a callback for when an asynchronous operation has finished

If you have any specific situations you'd like an example of, that would be easier to answer.

EDIT: I should point out that it's good news that you're only working with LINQ to Objects and LINQ to XML at the moment, as that means you don't need to understand expression trees yet. (They're cool, but one step at a time...) LINQ to XML is really just an XML API which works nicely with LINQ - from what I remember, the only times you'll use delegates with LINQ to XML are when you're actually calling into LINQ to Objects. (That's very nice to do, admittedly - but it means you can reuse what you've already learned.)

As you've already got C# in Depth, chapters 10 and 11 provide quite a few examples of using lambda expressions (and query expressions which are translated into lambda expressions) in LINQ. Chapter 5 has a few different examples of delegate use.

like image 22
Jon Skeet Avatar answered Oct 29 '22 00:10

Jon Skeet


Read this...

http://linqinaction.net/

..and all you're question will be answered!!!

like image 44
matt_dev Avatar answered Oct 28 '22 22:10

matt_dev