Skip to main content

1.3 - Understanding the .NET Ecosystem

What is .NET?

.NET (pronounced "dot net") is a free, open-source, cross-platform developer platform created by Microsoft for building many different types of applications. It provides the runtime environment and libraries that C# programs need to execute.

Think of .NET as the foundation and infrastructure that supports your C# code, similar to how a game engine provides the foundation for game development.

The .NET ecosystem consists of several key components:

  1. Programming Languages: C#, F#, and Visual Basic are the primary languages that can be used with .NET.

  2. Runtime: The environment where your code executes.

  3. Libraries: Pre-written code that provides common functionality.

  4. Development Tools: Software used to write, test, and deploy .NET applications.

The Common Language Runtime (CLR)

The Common Language Runtime (CLR) is the execution engine of .NET. It's responsible for running your C# code and providing services such as:

  • Memory Management: Automatically allocating and freeing memory.
  • Garbage Collection: Reclaiming memory from objects that are no longer in use.
  • Exception Handling: Managing errors that occur during execution.
  • Thread Management: Handling concurrent execution of code.
  • Type Safety: Ensuring that operations are performed only on compatible types.

How C# Code Executes

When you write C# code, it goes through several steps before it actually runs:

  1. Source Code: You write your program in C# (.cs files).

  2. Compilation: The C# compiler (csc.exe) converts your source code into Intermediate Language (IL) code and creates an assembly (.dll or .exe file).

  3. Just-In-Time (JIT) Compilation: When your program runs, the CLR's JIT compiler converts the IL code into native machine code that your computer's processor can execute.

  4. Execution: The native code runs on your computer.

This process is often referred to as "compile once, run anywhere" because the same IL code can be JIT-compiled and executed on any platform that has a compatible CLR implementation.

Assemblies in .NET

An assembly is the fundamental unit of deployment in .NET. It's a compiled code library used for deployment, versioning, and security.

Assemblies can be:

  • Executable (.exe): Contains code that starts execution at a designated entry point.
  • Library (.dll): Contains code that can be called by other assemblies but doesn't have an entry point.

Assemblies contain:

  • IL Code: The compiled code that will be executed.
  • Metadata: Information about the types, methods, and other elements defined in the code.
  • Manifest: Details about the assembly itself, including its version, dependencies, and security requirements.

Managed vs. Unmanaged Code

In the .NET world, code is categorized as either managed or unmanaged:

Managed Code

  • Executes under the supervision of the CLR
  • Benefits from CLR services like garbage collection and exception handling
  • Is typically written in languages like C#, F#, or Visual Basic
  • Has access to the .NET Framework or .NET Core libraries

Unmanaged Code

  • Executes directly by the operating system, outside the CLR
  • Manages its own memory and resources
  • Is typically written in languages like C or C++
  • Doesn't have direct access to .NET libraries without special interop mechanisms

C# primarily produces managed code, which is one reason it's easier to work with than languages that produce unmanaged code—many low-level details are handled for you.

.NET Implementations

Over the years, .NET has evolved into several implementations:

  1. .NET Framework: The original Windows-only implementation.

  2. .NET Core / .NET 5+: A cross-platform, open-source implementation that has now evolved into simply ".NET" (starting with .NET 5).

  3. Mono: An open-source implementation originally aimed at bringing .NET to platforms beyond Windows.

  4. .NET Standard: A specification that defines a set of APIs that all .NET implementations must provide.

Unity Context

Unity uses a specific .NET profile based on the Mono implementation. This profile may not include all the latest .NET features, which is why this course focuses on C# features up to version 9 that are widely supported in Unity.

Unity has been gradually updating its .NET support over the years. As of Unity 6.x, it uses a more modern .NET profile that supports most C# 9 features, but it's always good to check the Unity documentation for the specific version you're using.

Common .NET Libraries

.NET provides a vast collection of libraries (also called frameworks or APIs) that offer pre-built functionality:

  1. Base Class Library (BCL): Fundamental types and utilities like collections, file I/O, and string manipulation.

  2. ASP.NET: For building web applications and services.

  3. Windows Forms and WPF: For building desktop applications on Windows.

  4. Entity Framework: For database access.

  5. LINQ (Language Integrated Query): For querying data from various sources.

In this course, we'll primarily use the Base Class Library and occasionally touch on LINQ and other libraries relevant to game development.

The .NET Ecosystem and Unity

Unity has its own relationship with .NET:

  1. Unity's .NET Profile: Unity uses a customized version of .NET based on Mono, which may not include all features of the latest .NET versions.

  2. Unity API: Unity provides its own extensive API that interacts with the .NET framework.

  3. Script Compilation: Unity compiles your C# scripts into assemblies that run within its environment.

  4. Performance Considerations: Unity has specific optimizations and limitations related to garbage collection and memory management that affect how you write C# code.

Throughout this course, we'll focus on C# features that are well-supported in Unity and highlight any specific considerations for Unity development.

Conclusion

Understanding the .NET ecosystem provides important context for your C# learning journey. While you don't need to master all these concepts right away, having a basic awareness of how C# fits into the larger .NET picture will help you make sense of documentation and troubleshoot issues as you progress.

In the next section, we'll get practical and set up your development environment so you can start writing C# code!