8

I'm no newb when it comes to the .NET framework, I've been writing C# software that utilizes it for several years. However, a thought occurred to me the other day...how is C# separate from the .NET framework with keywords like lock and object? A statement like object derp = new object(); resolves to System.Object derp = new System.Object();, but System.Object is defined as part of .NET is it not? Even if it is defined in mscorlib, this means that a language feature is dependent on a library. The same goes for lock statements which resolve to calls to Monitor.Enter() and Montior.Exit(). Obviously it would be silly/impossible to write any non-toy program without using .NET features, but how can a language depend on a library that is largely written in that language?

Rabadash8820
  • 2,328
  • 3
  • 27
  • 49
  • 1
    `lock` is a C# keyword. `Monitor` is a C# class which is part of the .NET Framework. The c# compiler replaces the `lock` block with `Montior.Exit()` and `Montior.Enter()`. – Zein Makki Aug 11 '16 at 05:58
  • 1
    Relevant: http://stackoverflow.com/questions/2724864/what-is-the-difference-between-c-sharp-and-net/2724872#2724872 – Will Ray Aug 11 '16 at 06:06
  • 2
    The C# language doesn't depend on the .NET Framework. The C# language depends on the C# compiler and the set of the C# language specifications. Not to mention the rules of the Common language Runtime (Which has a Microsoft implementation). It is worth noting that a big part of the .NET framework calls native c++ classes. – Zein Makki Aug 11 '16 at 06:09
  • Your question is a bit unclear. With your final "how can a language depend on a library that is largely written in that language?", you might be looking for the concept of [Bootstrapping](https://en.wikipedia.org/wiki/Bootstrapping_(compilers)) – Damien_The_Unbeliever Aug 11 '16 at 06:38
  • 1
    You don't need the frame work classes. You can wright them all your self in C#! You will notice if you use an older .net framework that you miss classes and functions and that you have to create them your self. The same applies for c++ and probably all other languages. The frameworks are there just so you don't have to reinvent the wheel every time. – Jeroen Doppenberg Aug 11 '16 at 07:15
  • See also https://stackoverflow.com/questions/14039214/cs-linq-and-net-framework-which-one-depends-on-the-other. Short version is: where you think C# _depends_ on .NET, it's really just that the specification provides certain "hooks" that could be implemented by any compliant runtime. The specification defines type aliases like `object` and `int`, as well as dictates what more basic C# code statements like `lock` and `foreach` are equivalent to. – Peter Duniho Aug 11 '16 at 07:19
  • 1
    If after reviewing the marked duplicate, other related Q&A, and of course **the language specification**, you still have some _specific_ question you're having trouble understanding, please feel free to post a new question in which you explain precisely what you've already learned about the relationship between C# and .NET, and what specifically you're still having trouble comprehending. – Peter Duniho Aug 11 '16 at 07:20
  • I've added an answer to the question that this is marked as a duplicate of that you might find interesting, explaining how the language of C# can be completely separated from .net and how the .net library could (and - initially - probably did) exist without C# - it might answer your variation on the question more fully: http://stackoverflow.com/a/38890100/3813189 – Dan Roberts Aug 11 '16 at 07:37
  • Thanks for the replies everyone! @Peter I think I understand now. Is it basically like the c# language specification includes the lock and object keywords, and then compiler authors decide what these keywords resolve to (e.g. a class in .net or a class in mono)? So the c# compiler that you use in Visual Studio IS dependent on .net? – Rabadash8820 Aug 11 '16 at 08:32
  • _"So the c# compiler that you use in Visual Studio IS dependent on .net?"_ -- only as a matter of practicality. All the compiler does is generate IL. As long as you use a compatible runtime that will run the IL in the way expected by the compiler, it will work. It doesn't have to be .NET. It's similar, a little bit, to the way that Intel and AMD CPUs both execute the same x86 machine code. Two completely different manufacturers, adhering to the same specification. – Peter Duniho Aug 11 '16 at 15:00

1 Answers1

7

Minimum requirement to implement a language is to have a compiler to convert the language into executable code and a runtime system to execute it. Neither were written in C#, they used C++. Using another language to accomplish this is a traditional and necessary bootstrapping step. The C language is a very common choice for a bootstrapping language, it is small and very easy to port, the approach taken by Mono. C itself had a bootstrapping language, it was B. Which was bootstrapped off BCPL. Which was bootstrapped off CPL. Which was bootstrapped off Atlas2. Gets murky before that, imagine somebody using the toggle switches on the front panel of a very primitive computer.

Once that's in place you can start iterating and improving. Like adding support for the lock keyword, a language feature you don't need to get a compiler and runtime system going. It relies on the Monitor class, still very thin today with the vast majority of its code in C++.

Rewriting the compiler into the target language is often next, took quite a while for C# but now done with Roslyn. Rewriting the runtime system might be considered although that isn't very common. Done as a incubation project inside Microsoft, the secret Midori project. Not just the runtime but an entire operating system. Bootstrapped off the Singularity project.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • If we continue the chain, where do you reach ? Why did you stop at `Atlas2` ? That was an interesting chain. – Zein Makki Aug 11 '16 at 07:21
  • 1
    It is murky, the distinction between language and runtime gets fuzzy, Atlas2 was a computer design, bootstrapped off Atlas, bootstrapped of Titan. Computers having a dedicated language to program them was common in the olden days. Think Eniac, "programmed" by plugging cables. – Hans Passant Aug 11 '16 at 07:31
  • Very interesting history lesson @Hans, I've always wondered what languages were used to create other languages. But I'm not sure this answers my question. Sounds like you're saying that the Monitor class was originally written in C++, and Microsoft's c# compiler just replaces uses of the lock construct with appropriate calls to Monitor's functions. So .net didn't require c#, but what I'm asking is, why does a language feature depend on a pre-existing library? – Rabadash8820 Aug 11 '16 at 08:39
  • You missed the gist of the answer, language features depend on the compiler. What the runtime system does to actually implement the feature (if necessary) commonly depends on another language Like `lock` requiring a lot of C++ code. Not a library, this C++ code was written specifically to implement Monitor. – Hans Passant Aug 11 '16 at 08:49