C # LINQ and .NET Framework, which depends on another?

I think of the C # language compiler as a standalone black box, able to understand the text of a certain syntax and create compiled code. The .NET framework, on the other hand, is a massive library that contains functionality partially written in C # and partially C ++. Thus, the .NET environment depends on the C # language, and not vice versa.

But I can't fit this into how LINQ works. LINQ queries are specific syntax text that the C # compiler can understand. But to create my own LINQ provider, I need to work with interfaces such as IQueryable and IQueryProvider, both of which are defined in the System.Linq namespace for the framework.

Does this mean that C # feature offerings depend on part of the .NET platform? Does C # know about the .NET platform?

+2
c # linq
source share
2 answers

LINQ queries are specific syntax text that the C # compiler can understand.

Well, query expressions - but the compiler really doesn't "understand" them. He simply translates them quite mechanically. For example, take this query:

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

This is translated into:

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

The compiler knows nothing about the meaning of Where and Select . They don’t even have to be methods - if you have the appropriate fields or properties for delegate types, the compiler will be fine with it. Basically, if the second form is compiled, so will the query expression.

Most LINQ providers use extension methods to provide these methods ( Where , Select , SelectMany , etc.). Again, they are just part of the C # language - the compiler does not know and does not care about what the extension methods do.

For more information on how query expressions are translated, see part 41 of my Edulinq blog series . You can also find the rest of my Edulinq series - this is basically a series of blog posts where I redefine LINQ for objects, one method at a time. Again, this demonstrates that the C # compiler does not rely on the LINQ implementation in the System.Linq namespace or something like that.

+7
source share

The .NET Framework contains many parts. One of the most important is the CLR - Common Language Runtime. All .NET languages ​​depend on it, including C #, because they create IL code that cannot be executed by a machine processor. Instead, the CLR executes it.

And there is also a base class library, BCL, which is available for use in any .NET language: C #, VB.NET, Managed C ++, F #, IronRuby, you name it. I doubt it was written in C #. It does not depend on any features of these languages, because classes and OOP are built into the CLR environment.

So, yes, C # knows about the .NET platform, it must know about it. Think about IEnumerable : to compile foreach into GetEnumerator() and MoveNext() calls, the C # compiler needs to know that, well, IEnumerable exists. And a little special.

Or think of attributes! The C # compiler has internal knowledge of which methods the Attribute interface provides.

But the CLR itself knows nothing about C #. For everyone.

+8
source share

All Articles