Assemblies in .NET

An assembly is the fundamental unit of deployment, versioning, reuse, activation, and security in the .NET ecosystem. It's a collection of types and resources that are built to work together and form a logical unit of functionality. Every .NET application, whether it's a simple console application or a complex enterprise system, is composed of one or more assemblies.

What an Assembly Contains

An assembly is the smallest deployable unit in the .NET Framework. It's typically packaged as a DLL (Dynamic Link Library) or an EXE (executable) file. Internally, an assembly is composed of:

Key Characteristics and Benefits

Types of Assemblies

Assembly Loading

When an application needs to use types from another assembly, the CLR is responsible for locating and loading that assembly into memory. This process involves:

  1. Checking the application's configuration files for assembly binding information.
  2. Searching the application's directory and the Global Assembly Cache (GAC).
  3. If found, the assembly is loaded into the application domain.
  4. The CLR performs verification and JIT compilation of the code before it can be executed.

Example of referencing an assembly:

In your C# project, you typically add a reference to another assembly. This tells the compiler that the types defined in that assembly are available for use in your code. For instance, referencing the System.Text.Json assembly would allow you to use its JSON serialization/deserialization capabilities.

// Example in C#
            using System.Text.Json;

            public class Person {
                public string Name { get; set; }
                public int Age { get; set; }
            }

            public class Program {
                public static void Main(string[] args) {
                    var person = new Person { Name = "Alice", Age = 30 };
                    string jsonString = JsonSerializer.Serialize(person);
                    System.Console.WriteLine(jsonString);
                }
            }
            

Further Reading