The Difference Between C# and .NET Through The Lens Of JavaScript

Jul 18, 2019
Adam Sheaffer

There’s one question that all students wonder about that we don’t always have time to discuss in class: What’s the difference between C# and .NET? Students hear instructors use the terms seemingly interchangeably and plenty of them probably write the two off as being the same. But they’re not. So how do you explain the difference to a new programmer whose only experience is a few months of writing JavaScript? 

If you google the question, you’ll find plenty of answers, but they don’t seem to offer much clarity to a student. For example, if you search for the “difference between C# and .NET” on Stack Overflow, this is the top-rated answer:

C# is a programming language, .NET is a blanket term that tends to cover both the .NET Framework (an application framework library) and the Common Language Runtime which is the runtime in which .NET assemblies are run.

This is a solid answer, but if you’re a student who has just started our back-end course, you probably have more questions than when you started searching for an answer. So here’s yet another blog post on the same topic. The difference here is that this is geared towards new programmers whose experience is only a few months of writing JavaScript and are just beginning to look at .NET development. By explaining how JavaScript works, the goal is to add the context needed to understand this Stack Overflow answer.

What is a Runtime Environment?

Before students start at NSS, we make sure they already have brief exposure to JavaScript. We currently ask them to go through about 40 hours worth of online JavaScript courses on Team Treehouse. Students watch videos and then complete exercises in an editor that’s embedded in the Treehouse site. Earlier this year I talked with a soon-to-be NSS student who had just finished the Treehouse courses and was feeling confident enough to start writing some code on his own. He installed a text editor and wrote his first simple JavaScript program that created some variables and logged some messages to the console. After he wrote it, he sat there for a while until he had to ask, “How do I run this??”

That question came with the realization that code is just text. The only thing that makes it “JavaScript” is that it has a .js extension on it and that the syntax follows the arbitrary rules that were created for that language: Curly braces go here. Quotes go here. Parenthesis here. The code is meaningless without a runtime environment--a program that understands that syntax and knows how to execute it. This program (or “engine”) is built into each browser, and that’s why we can run JavaScript inside all of our browsers. However, each browser implements its own engine, which is why we sometimes experience slightly different behavior when we run applications in different browsers. 

There is a group of people who are in charge of managing the JavaScript language. Every once in a while, they decide they want to change things about it. Remember how all of your older JS materials use `var` instead of `const` and `let`, or how they use the `function` keyword instead of using fat arrows? These are new features that have been recently added to the language. It’s important to remember though that these new features are meaningless until the environments in which this code is run are updated to know how to interpret this new syntax. 

Core Library and Built-In JavaScript Objects and Classes

Programming languages will usually come out of the box with some built-in classes and methods. Before we start any new JavaScript application we already have a few objects and classes that we have access to. Things like 

Promises, 
static methods like `Math.random` and `JSON.parse`, 
Arrays and Array methods like map, filter, and reduce,
Etc, etc….

Runtimes and core libraries go hand-in-hand. We wouldn’t get very far writing a JavaScript application if classes like Strings and Arrays weren’t already implemented for us.

Back to C# and .NET

Now that we hopefully have a better grasp on what runtime environments and core libraries are, let’s revisit the original answer to our question.

C# is a programming language, .NET is a blanket term that tends to cover both the .NET Framework (an application framework library) and the Common Language Runtime which is the runtime in which .NET assemblies are run.

C# is the language. It’s the rules around what keywords are available (i.e. “class”, “namespace”, “return”, etc); where curly braces go; how classes and objects work; etc. 

The .NET runtime is what actually knows how to execute our compiled code. .NET is also referred to as a Common Language Runtime (CLR). This is because in addition to the C# language, the .NET runtime can also execute compiled code written in F# and VisualBasic.

And while the JavaScript core library offers us a handful of built-in functionality, .NET comes with thousands of classes right out of the box for us to immediately start using. For example, the `List` class is something we immediately become familiar with as we start writing applications in C#. The List class is already available for us to use and already has methods like `Add` and `Remove` implemented. These class libraries are what make up the .NET Framework.

So there you have it. .NET refers to both the out-of-the-box class library that Microsoft has built for us, along with the ability to execute our compiled C# code.

Topics: Learning, Technology Insights