C#的LINQ和.NET Framework,哪个依赖另一个? [英] C#'s LINQ and .NET Framework, which one depends on the other?

查看:180
本文介绍了C#的LINQ和.NET Framework,哪个依赖另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为C#语言编译器是一个独立的黑盒子,能够理解某种语法的文本并生成编译后的代码.另一方面,.NET框架是一个庞大的库,其中包含部分由C#和部分由C ++编写的功能.因此,.NET框架依赖于C#语言,而不是其他方式.

I think of C# language compiler as a self contained black box capable of understanding text of a certain syntax and producing compiled code. On the other hand .NET framework is a massive library that contains functionality written partly by C# and partly by C++. So .NET framework depends on C# language, not the other way around.

但是我不适合LINQ的工作方式. LINQ查询是C#编译器可以理解的特定语法的文本.但是要通过自己的LINQ提供程序进行构建,我需要使用IQueryable和IQueryProvider之类的接口,它们都在框架的System.Linq命名空间中定义.

But I cannot fit this into how LINQ works. LINQ queries are text of a particular syntax that C# compiler can understand. But to build by own LINQ provider I need to work with interfaces like IQueryable and IQueryProvider both of which are defined in System.Linq namespace of the framework.

这是否意味着C#语言提供的功能取决于.NET框架的一部分? C#语言是否了解.NET框架?

Does that mean a functionality C# language offers is dependent on a part of .NET framework? Does C# language know about .NET framework?

推荐答案

LINQ查询是C#编译器可以理解的特定语法的文本.

LINQ queries are text of a particular syntax that C# compiler can understand.

好吧,查询表达式是-但是编译器并没有真正理解"它们.它只是以一种非常机械的方式翻译它们.例如,使用以下查询:

Well, query expressions are - but the compiler doesn't really "understand" them. It just translates them in a pretty mechanical manner. For example, take this query:

var query = from foo in bar
            where foo.X > 10
            select foo.Y;

该词翻译为:

var query = bar.Where(foo => foo.X > 10)
               .Select(foo => foo.Y);

编译器对于WhereSelect的含义一无所知.它们甚至不必是方法-如果您具有委托类型的适当字段或属性,则编译器会很好用.基本上,如果第二种形式可以编译,查询表达式也将编译.

The compiler doesn't know anything about what Where and Select mean here. They don't even have to be methods - if you had appropriate fields or properties of delegate types, the compiler would be fine with it. Basically, if the second form will compile, so will the query expression.

大多数LINQ提供程序都使用扩展方法提供这些方法(WhereSelectSelectMany等).同样,它们只是C#语言的一部分-编译器不知道或不在乎扩展方法做什么.

Most LINQ providers use extension methods to provide these methods (Where, Select, SelectMany etc). Again, they're just part of the C# language - the compiler doesn't know or care what the extension methods do.

有关如何翻译查询表达式的更多详细信息,请参见

For more details about how query expressions are translated, see part 41 of my Edulinq blog series. You may find the rest of my Edulinq series informative, too - it's basically a series of blog posts in which I reimplement LINQ to Objects, one method at a time. Again, this demonstrates that the C# compiler doesn't rely on the LINQ implementation being in the System.Linq namespace, or anything like that.

这篇关于C#的LINQ和.NET Framework,哪个依赖另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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