代表团和兰布达斯和LINQ,哦我的! [英] Delegates and Lambdas and LINQ, Oh My!

查看:224
本文介绍了代表团和兰布达斯和LINQ,哦我的!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个相当初级的开发人员,我遇到了一个问题,突出了我的缺乏经验和我的知识中的漏洞。请原谅我,如果这里的序言太长了。



我发现自己在一个项目,涉及到我需要学习一些新的(对我)技术,包括LINQ (针对本项目的目标对象和XML)等。我已经读到的一切都表明,要使用LINQ,我需要充分了解以下内容(代理,匿名方法和Lambda表达式)。



现在开玩笑了过去,我已经使用了CONSUMED代表,因为我已经使用了.NET事件模型,但是大部分的细节都被我隐藏了(感谢Microsoft!)。我明白,在基本层面上,委托实例是指向方法的指针(我知道的总体过度简化)。



我明白匿名方法本质上是一般而言(如果不是排他地址)被创建为代理的目标。



我还明白,lambdas以不同的方式用简单的语法使用,可以使用将一个简单的匿名方法指向代表。



如果我的任何描述都在这里,请谅解我,这是我理解这些主题的基本级别。



所以,挑战:


  1. 任何人都可以告诉我至少在基本层面上,如果我对这些项目的了解甚至相近?我不是在寻找复杂的深奥细节,只是基础(现在)。


  2. 在应用LINQ之前,我需要在多大程度上真正了解这些概念在一个项目合理的效果?我想要充分了解,愿意花时间。我可能没有时间充分挖掘所有这些东西,我需要做一些工作。


  3. 任何人都可以指出一些很好的文章,解释这些科目并将其应用于现实世界的例子,以便我能够围绕主题和应用的基础知识?我的意思是现实世界,我如何在客户和发票的上下文中使用它,而不是抽象的矢量和形状或动物和奶牛。这种情况可能有些为演示而设计,但希望不是严格的学术。我已经发现了一些在线和书籍的例子,但很少有人似乎是简单的英语解释。


感谢大家的耐心,时间和专业知识。

解决方案

1)这些说明听起来对我来说非常准确。有时匿名方法和lambda表达式将需要创建一个新的类型来放置委托的目标,所以他们可以作为关闭



2/3)我会读一点,直到你对代理,匿名方法和lambda表达式感到满意。我在C#深度的C#2.0和C#3.0的每一个代理相关变更中都讲了一章,尽管其他的书籍也有详细的介绍。如果这有帮助,我也有文章



至于示例 - 代表用于许多不同的目的。它们都是不同的方式来查看相同的功能,但是他们可以感觉非常不同:




  • 提供在开始新线程时调用的代码

  • 对UI事件进行响应

  • 为LINQ查询提供过滤器,选择,排序等

  • 为异步操作完成时提供回调



如果您有任何特定情况, d喜欢一个例子,这将更容易回答。



编辑:我应该指出,这是一个好消息,你只是使用LINQ to Objects和LINQ到目前为止,这意味着您不需要了解表达式树。 (他们很酷,但是一次只有一步...)LINQ to XML真的只是一个XML API,可以很好地与LINQ一起工作 - 从我记得,唯一一次你将使用LINQ to XML的代理您实际上正在调用LINQ to Objects。 (这是非常好的,不可否认 - 但这意味着你可以重复使用你已经学到的东西。)



由于你已经在深度C#,第10章和11提供了很多使用 lambda表达式(以及被翻译成lambda表达式的查询表达式)在LINQ中的示例。第5章有几个代表使用的例子。


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.

解决方案

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.

这篇关于代表团和兰布达斯和LINQ,哦我的!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆