C#
.NET
Assembly
Software Development
Programming

What exactly is an Assembly in C or .NET?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In the context of C# and .NET, an Assembly is a fundamental building block of any application. It represents a compiled code library used by .NET applications. These assemblies are created whenever a .NET program is built, serving as containers for the types and resources of a .NET application.

Assemblies ensure version management and facilitate the deployment of applications by conveniently combining code and resources within a single package.

Technical Overview

An assembly in .NET is the smallest unit of deployment, version control, security permissions, and reference packaging. Assemblies typically have four main characteristics:

  1. Versioning Information: Assemblies maintain versioning information, which helps manage changes and updates over time.
  2. Public Types: Assemblies encapsulate a collection of types defined in the Common Language Runtime (CLR).
  3. Resources: Assemblies can include resources such as strings, images, and files.
  4. Manifest: Assemblies contain metadata called a manifest, which includes the assembly's name, version number, culture settings, and public key if it's a strongly named assembly.

Types of Assemblies

  1. Static Assemblies: These are simply created when a .NET application is compiled. These are typically stored as `.dll` or `.exe` files. Static assemblies allow code reuse across multiple projects.
  2. Dynamic Assemblies: Unlike static assemblies, dynamic assemblies are either saved to disk or executed directly from memory. They are created programmatically at runtime and are useful for scenarios involving on-the-fly code generation.

Assembly Components

An assembly comprises several key components:

  • Manifest: Contains metadata about the assembly like name, version, culture, and information about referenced assemblies.
  • Type Metadata: Provides metadata about each type defined within the assembly including classes, interfaces, and value types.
  • Intermediate Language (IL) Code: CPU-independent code that the CLR compiles to native machine code at runtime.
  • Resources: Files that include static resources such as images and localized language strings.

Example

Here's a simple example to illustrate how an assembly is created in C#. Consider this C# code file, `HelloWorld.cs`:

  • Implicit Loading: Occurs automatically when the application starts.
  • Explicit Loading: Managed by code through reflection, often leveraging the `System.Reflection.Assembly` class.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.