Mastering C# Part 1.1

Mastering C# Part 1.1

C# in-depth advanced tutorial series

Hello, and welcome to the C# Adventure Series!

Whether you're a coding novice or a seasoned developer exploring a new language, you're in the right place. C# (pronounced C-Sharp) is a modern, powerful, and incredibly versatile programming language developed by Microsoft as part of its .NET initiative.

Imagine a world where you can build virtually anything—interactive websites, sleek mobile apps, immersive games, and even cloud solutions—all with the same language. That’s the magic of C#. It’s designed to be simple to learn, safe to use, and endlessly scalable.

In this series, we’ll guide you step-by-step through the wonderful world of C#, covering its syntax, features, and how to use it effectively with the .NET Framework and beyond. Whether you're dreaming of building a game, creating web applications, or automating tasks, you’ll find yourself empowered by the capabilities of C#. Let’s get started !

C# (pronounced "C-Sharp") is a modern, versatile, and object-oriented programming language developed by Microsoft in 2000 as part of its .NET initiative. It is designed to be simple, type-safe, and scalable, making it ideal for building a wide range of applications, including web, desktop, mobile, cloud, and gaming solutions. C# runs on the .NET Framework, which provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. With its syntax style similar to C, C++, and Java, C# is both familiar and enhanced for developer productivity.

The .NET Framework consists of two main components: the Common Language Runtime (CLR) and the .NET Framework Class Library. The CLR serves as a virtual machine, managing the execution of code written in supported languages like C# and VB.NET. It includes features such as code access security, automatic memory management, and just-in-time (JIT) compilation, which improve application reliability, security, and performance.

Meanwhile, the .NET Framework Class Library offers a vast collection of pre-built functions and classes, enabling developers to create robust and scalable applications. Together with C#’s modern features, such as LINQ, async/await, and functional programming support, the .NET Framework ensures a powerful and efficient platform for application development.

How C# actually works behind the scene?

  • When the application runs, the Common Language Runtime (CLR), which serves as the execution environment for the .NET Framework, processes the MSIL code in several steps:

    • Source Code: You start with your high-level source code written in a .NET programming language such as C# or VB.NET.

    • Compilation to MSIL: The source code is compiled by the language-specific compiler (like csc for C#) into Microsoft Intermediate Language (MSIL), also known as Common Intermediate Language (CIL). MSIL is platform-independent and serves as an intermediate representation of the program.

    • CLI Assembly: The MSIL code, along with metadata (information about the types, methods, and other elements), is bundled into a Common Language Infrastructure (CLI) assembly. This assembly can either be a standalone executable file (.EXE) or a dynamic-link library (.DLL), which contains the MSIL code ready for execution.

    • JIT Compilation: When the application is run, the Common Language Runtime (CLR) loads the CLI assembly. The Just-in-Time (JIT) compiler then dynamically compiles the MSIL code into native machine code specific to the hardware platform (like x86 or ARM) and operating system. This compilation happens at runtime, allowing the application to be executed.

    • Execution: The CPU executes the native machine code generated by the JIT compiler, performing the operations defined in the original source code. This results in the application running and producing the desired output, such as displaying a message or performing a calculation.

1. Source Code (C#)

This is the C# source code for a simple console application:

    using System;

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello, World!");
        }
    }

2. Compilation to MSIL

When you compile the C# code using the csc compiler (csc Program.cs), it gets converted into Microsoft Intermediate Language (MSIL), which is a low-level, platform-independent code. You won’t see MSIL directly unless you decompile the compiled EXE file.

But the process looks something like this behind the scenes. The C# code is turned into MSIL (in a simplified form):

    .assembly Program
    {
        .ver 1:0:0:0
    }

    .module Program.exe

    .class public auto ansi beforefieldinit Program
           extends [mscorlib]System.Object
    {
        .method public hidebysig static void Main() cil managed
        {
            .entrypoint
            .maxstack 8
            .locals init ([0] object V_0)
            IL_0000: nop
            IL_0001: ldstr "Hello, World!"
            IL_0006: call void [mscorlib]System.Console::WriteLine(string)
            IL_000b: nop
            IL_000c: ret
        }
    }

This is the compiled MSIL code stored inside the Program.exe assembly.

3. CLI Assembly

The Program.exe file is now a CLI (Common Language Infrastructure) assembly containing both the MSIL code and metadata (information about types, methods, etc.). This assembly can be executed by the Common Language Runtime (CLR).

4. JIT Compilation (At Runtime)

When you run the Program.exe file, the CLR loads the MSIL code from the assembly and passes it to the Just-In-Time (JIT) compiler. The JIT compiler converts the MSIL into native machine code specific to the target platform.

For example, on an x86 architecture, the native machine code might look like assembly instructions like this (simplified):

    ; Assembly code that would be executed by the CPU
    mov eax, offset "Hello, World!"    ; Load the string address into a register
    call Console_WriteLine              ; Call the method to print the string

5. Execution

After JIT compilation, the native machine code is executed by the CPU. The result of running the Program.exe is that the text "Hello, World!" is displayed on the console.

    Hello, World!
  1. C# codeMSIL (compiled by csc)

  2. MSILCLI Assembly (stored in Program.exe)

  3. JIT CompilationNative machine code (compiled at runtime for the specific platform)

  4. Execution → The native code runs on the CPU and outputs "Hello, World!" on the screen.

This process ensures that the code is platform-independent (thanks to MSIL) but optimized for the specific machine at runtime (via JIT compilation).

What is the difference between .NET and .NET Framework?

.NET and .NET Framework share many of the same components and you can share code across the two. Some key differences include:

  • .NET is cross-platform and runs on Linux, macOS, and Windows. .NET Framework only runs on Windows.

  • .NET is open-source and accepts contributions from the community. The .NET Framework source code is available but doesn't take direct contributions.

  • All of the innovation happens in .NET.

  • .NET Framework is included in Windows and automatically updated machine-wide by Windows Update. .NET is shipped independently.

From its inception in 2000 as part of Microsoft's .NET initiative to its evolution into one of the most versatile and widely-used programming languages, C# has come a long way. Its modern features, robust tools, and supportive ecosystem make it a go-to choice for developers around the globe.

Now that we know where C# comes from and why it’s so powerful, it’s time to roll up our sleeves and dive in!

Let’s start learning C# together—from writing your very first program to mastering advanced concepts. Ready to begin your journey? Let’s head straight into the exciting world of C# programming!