LINQ、Lambda、匿名方法、委托
谁能解释一下 LINQ、Lambda、匿名方法、委托是什么意思?
这三个有何不同?
一个可以替换另一个吗?
当我谷歌搜索时我没有得到任何具体答案
Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant?
How these 3 are different for each other?
Was one replaceable for another?
I didn't get any concrete answer when i did Googling
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LINQ 是一个广泛的技术名称,涵盖了 .NET 3.5 和 C# 3.0 的大部分变化; “用语言查询”等等。
委托相当于函数指针;如果您愿意的话,可以将“方法句柄”作为对象,即
一种编写我可以调用的委托的方式。委托还支持事件处理和其他回调方法。
匿名方法是创建委托实例的 2.0 简写,例如:
它们还通过“捕获的变量”(上面未显示)将完整的闭包引入到语言中。 C# 3.0 引入了 lambda,它可以产生与匿名方法相同的结果:
但它也可以编译到表达式树中,以针对(例如)数据库进行完整的 LINQ。例如,您不能针对 SQL Server 运行委托!但是:
可以转换为 SQL,因为它被编译为表达式树 (
System.Linq.Expression
)。因此:
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.
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:
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:
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:
can be translated into SQL, as it is compiled into an expression tree (
System.Linq.Expression
).So: