The provided content is a comprehensive guide that distinguishes C# as a programming language from the .NET framework, detailing their history, differences, and the functionalities they offer within the Microsoft ecosystem.
Abstract
The text serves as an introduction to C# and .NET, explaining that C# is a language developed by Microsoft, similar to Java, while .NET is the framework that enables C# code to execute on various operating systems. It outlines the evolution of C# and .NET, including Microsoft's initial attempt to compete with Java, leading to the creation of C# and the .NET Framework. The guide highlights the current state of C# and .NET, emphasizing their popularity, versatility, and the range of applications they support, from backend development to gaming. It also delves into the technical aspects of C#, such as its principles of object-oriented programming, strong typing, and features like async methods and LINQ. The document explains the compilation process of C# code into intermediate language (IL) and its subsequent execution by the Common Language Runtime (CLR), which manages memory and provides services like error handling and thread management. The text clarifies the distinction between executable (.exe) and library (.dll) files in C# and discusses the components of the .NET framework, including the CLR, the Framework Class Library (FCL), and the Base Class Library (BCL). It also touches on the .NET SDK and the introduction of Ahead-of-Time (AOT) compilation in .NET 8.
Opinions
The author suggests that C# and .NET are thriving technologies, supported by Microsoft's continuous investment in AI, cloud, and DevOps integrations.
There is an opinion that C# was created as a direct competitor to Java, with Microsoft aiming to offer a similar "write once, run anywhere" philosophy.
The text implies that the .NET Core's ability to run on multiple platforms (Linux, Windows, and macOS) was a significant improvement over the .NET Framework.
The author expresses that the Roslyn compiler has modernized C# development by providing advanced features and an open-source platform.
There is a view that the .NET SDK and NuGet packages offer a rich ecosystem, allowing developers to extend their applications with a wide range of functionalities.
The author seems to appreciate the CLR's role in managing the execution of .NET applications, particularly its garbage collection and error handling mechanisms.
The introduction of AOT compilation is presented as a positive development, potentially offering faster startup times and reduced memory footprints for .NET applications.
What is the difference between C# and .Net ?
This is a gentle introduction into C# and .Net
I hope this helps!
In Short…
C# is a programming language that is very similar to Java, it was created by Microsoft in 2000.
.Net (dotnet) is the framework that makes C# work on your computer
That’s it!
well, almost…
Let’s dive into it
Agenda
Introduction C# and .Net, a Microsoft Story
What is C#?
What is .Net?
What are the different .Net and C# versions?
What is the difference between .Net Framework, .Net Core and .Net?
How is C# code compiled?
What is the C# Compiler?
What is the difference between an executable (exe) and a library file (dll) in C#/ .Net?
What is the Common Language Runtime (CLR) in .net?
What are the components of the .net framework?
Introduction C# and .Net
C#
C# (pronounced C sharp), it was unveiled in 2000, the first version C#1.0 was released in January 2002.
The idea was to have a simple, modern, general purpose and object-oriented-language.
Although it stemmed from the C language family (C++ and C), it was (still is) meant to provide an alternative to Java. That’s why both look awfully similar.
A Microsoft story
In the late 90s, Microsoft, under the leadership of Bill Gates, dominated the tech landscape with its Windows monopoly. The U.S. Department of Justice accused Microsoft of employing a strategy known as “Embrace, extend, and extinguish,” where successful technologies were embraced, bundled with Windows, extended beyond the original standard, and rendered incompatible to drive users towards Windows and Microsoft products.
When Sun Microsystems introduced Java in 1995, Microsoft saw the potential and tried to implement this strategy. It introduced its own version of the Java Virtual Machine (JVM) with Internet Explorer 3 (IE3) but went beyond the Java standard. In 1997, Sun sued Microsoft for incompletely implementing the Java 1.1 standard, leading Microsoft to discontinue its implementation.
Rather than adopting Sun’s JVM, Microsoft opted to outpace Sun by creating its language and platform. Anders Hejlsberg, a renowned programming languages designer, was brought in to develop a language, unofficially dubbed a “better Java” in practice. This effort resulted in the birth of C# and the .NET framework.
Imagine wanting to write an application, but while doing that, you also need to code how the application is going to work with the operating system, the memory and everything else on your computer.
That’s when in the late ’90s, Microsoft initiated the development of the .NET framework with the goal of creating a platform centered around managed code — code executed under a runtime environment.
The idea is that the runtime will ensure that your code works under any OS/machine.
But Java was already doing it, having the philoshopy of ‘Write Once, Run everywhere’. So Microsoft had to strike back.
The inaugural release of the .NET Framework in 2002 marked the introduction of C#, a language for writing managed code with a design reminiscent of C++
TLDR:
In the 90s Java is doing great, Microsoft tries to copy Java and fails
Microsoft comes up with C# as a Java competitor and introduces .Net (a runtime)
.Net is meant to emulate Java philosophy of “Write once, run everywhere” i.e. Java code can run on all OS thanks to its runtime
// In C# we work with objects// Let's imagine we want to print on the Console a person's name// First we create an boject (aka a class)publicclassPerson
{
// Propertiespublicstring Name { get; set; }
}
// Now we can use this class to create a person and give a name
Person person1 = new Person(); // create object
person1.Name = "Bob"; // assign value to Name// The advantage are many, one of them reusability (aka creating many persons)
So while the Task GetPersonByName is making an API call, the main thread keeps doing its thing
Linq
Linq is like SQL but in C# code!
// 1st way to use LINQvar result = from person in people
where person.Age >= 30select person;
//2nd way to use LINQvar result = people.Where(person => person.Age >= 30);
while these two are the most popular features, the language keeps evolving and keeps adding new features (nullabe, Properties, Delegates and Events, Pattern matching…)
// ? for a nullableint? nullableInt = null;
// so easy to get/set variables in C#!publicstring Name { get; set; }
// creating an eventpublicdelegatevoidMyDelegate(string message);
publicevent MyDelegate MyEvent;
// Pattern matchingif (shape is Circle c)
{
// Code for handling Circle
}
At this point you are surely convinced that C# is amazing
What is .Net?
A developer platform meant to write all sorts of apps in a productive and safe manner. Its main components are:
A C# compiler
A runtime to execute C#
Libraries to provide out-of-the-box functionalities
The compile time happens when the application builds, the runtime happens when the application runs
1/ Developer writes C# code and clicks on build
2/ The C# compiler translates the C# source code into Common Intermediate Language (CIL)
The compiler is either known as csc.exe (in older versions) or Roslyn (current compiler)
3/ Intermediate Language (IL) Generation
The compiler takes all .cs files and generates an assembly containing IL, which is a low-level, platform-agnostic representation of the code.
IL is not machine code but is closer to machine code than the original C# source.
4/ Assembly Generation: the generated IL is stored in an assembly.
An assembly is a fundamental unit of deployment in .NET and can be either an executable (with the .exe extension) or a library (with the .dll extension).
Runtime
5/ Just-In-Time (JIT) Compilation: just before the application is executed, the IL is turned into native machine code
The Common Language Runtime (CLR) is responsible for this process.
6/ Execution of machine code. The .Net Runtime keeps running in the background and handles tasks such as memory, error handling…
Why is c# code first compiled into intermediate language (IL)?
Write Once, run everywhere
IL is a low-level, platform-agnostic representation of the code. When C# code is compiled into IL, it becomes independent of the underlying hardware and operating system.
This allows the same compiled code (IL) to be executed on any system that has a compatible Common Language Runtime (CLR).
2. Improved performance & security
When the application runs, the CLR on the target system performs JIT compilation to generate native machine code which then becomes optimised for the specific hardware architecture and local OS
Now you might think, class libraries and other projects are created with projects that end by .cs, but what are these?
CS for C Sharp
A file with a .cs extension in C# represents a C# source code file.
The .cs extension stands for "C Sharp".
When does a file become either an .exe or a .dll?
So you might wonder, how does it go from .cs to an .exe or .dll?
After the intermediate language generation (IL) stage, the code is turned into an assembly (.exe or .dll)
For .exe files, the CLR might run some optmisations based on the target OS
Here’s the process in detail
What is the Common Language Runtime (CLR) in .net?
The CLR is an environment that manages the execution of .Net applications
It comes into play at Runtime
When you run your app, these things happen:
The CLR triggers the JIT to compile your code into native code
While the app is running the CLR manages all the background tasks
What is Just In Time Compilation (JIT) in .net?
JIT happens when the app runs, JIT takes the Common Intermediate Language and translates it into machine code specific and optimised for the given OS.
JIT carries some important tasks:
Compiles CIL into binary code at runtime
Ensures that any OS can run the code
Optimises code for the host
Ensures that the CIL complies to Common Type System safety rules
Also, it is good to note that:
JIT Compilation occurs dynamically at runtime
The conversion of CIL to native binary code happens on-demand, right before the code is executed for the first time.
When JIT is used, not all code is necessarily compiled.
This is known as “lazy compilation” or “on-demand compilation.” Not all methods or functions are compiled at the beginning; rather, only the necessary parts are compiled as needed during the program’s execution.
Warm-up Period:
When an application starts, there might be a short delay as the JIT compiler generates native code for frequently used methods. This is sometimes referred to as the “warm-up” period. Subsequent executions benefit from the compiled code, resulting in improved performance.
What background tasks does the Common Language Runtime do?
Let’s look at the tasks of the CLR into more details (see previous version for JIT)
Manages Memory:
The CLR is responsible for memory management, including allocation and deallocation of memory for objects, as well as garbage collection to reclaim memory that is no longer in use.
Benefits:
Frees developers from having to manually release memory.
Allocates objects on the managed heap efficiently.
Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations.
Provides memory safety by making sure that an object can’t use for itself the memory allocated for another object.
The CLR is responsible for loading and executing assemblies, managing dependencies, and resolving references. It ensures that the required components are available and correctly linked during runtime.
Those are some of the features and tasks of the CLR, of course there are many more things that the CLR does!
We have just seen the whole process from c# code to machine code with the .Net Framework.
But that’s not all, there is so much more that .Net provides!
All .Net frameworks include one or more Common Language Runtime (CLR), an AOT runtime (CoreRT, in development), a Base Class Library (BCL), and the .NET SDK.
Let’s zoom on some main components that we haven’t touched upon yet