Delegates and Lambdas and LINQ, Oh My!

∥☆過路亽.° 提交于 2019-12-02 23:32:25

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.

Amy B

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.

Read this...

http://linqinaction.net/

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!