Assemblies in .NET

.NET Programming Tutorials

An assembly in .NET is a single deployment unit that contains a collection of types and references. Assemblies work as a building block in a .NET application. All the resources needed by an application are contained in an assembly, such as classes, structures, interfaces, and so forth.

An assembly is automatically created whenever a developer creates a new Windows app, web service, or class library. This assembly can be either a DLL (Dynamic Link Library) or an EXE (Executable file). We will discuss what these two are and how they differ later in this programming tutorial.

Read: Introducing the .NET Coding Pack

What is an Assembly in .NET?

Understanding assembly can sometimes be a tedious task, especially for a new .NET programmer. If you go to Microsoft.com, you will find the following definition of a web assembly:

“An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications”

In simple terms, an assembly in .NET is simply a precompiled chunk of code that can be run by the .NET runtime environment. A .NET program may contain one or more assemblies. Take a look at the following diagram to better grasp this concept:

.NET Assembly Explained

Before moving on to the core topic of this .NET programming tutorial, let us first take a brief look at the different forms of assembly.

What are DLL and EXE in the .NET Ecosystem?

The terms DLL and EXE are used commonly in programming and software development. When a developer is done building an application or project, they can either export their software as an EXE or DLL. But what, exactly, do these terms mean?

Let’s start by examining what an EXE is first, as it is the term you are likely more familiar with.

EXE is a short form for the word ‘executable’. In any .NET application package, there will be at least one EXE file that may or may not complement one or more DLL files. Exe files have a part in the code from where the program execution starts or, put another way, has an entry point for the execution of a program. Launching an EXE file creates its own memory space by the operating system.

On the flip side, DLL is an acronym for Dynamic Link Library, which contains functions and procedures that can be used by EXE files or libraries. As DLL is a library, you cannot execute it directly. Doing so will result in an error. Instead, a programmer can call a DLL from another program if he knows the function name and its signature in the DLL file.

Because of this capability, DLLs are a perfect fit for distributing device driver software. Unlike an EXE, a DLL does not create its own memory space; rather, they simply share the memory space of the calling application.

Read: Detecting and Preventing Memory Leaks in .NET

What are the Types of Assemblies in .Net?

In any .NET application, assemblies can be private or shared. In this section of our programming tutorial, we will cover the different types of assemblies and what purpose they serve in crafting an application.

Private Assembly

Private assemblies are private to a single application. To understand what a private assembly is, let’s examine it through the lens of an example. Suppose we have created a DLL that holds the business logic of an application. Only the client app needs this DLL and it is not required by other apps. To use the DLL with the client application properly, the DLL should be placed in the same folder in which the client application is residing. That way, this assembly works as a private assembly to the specific application.

Shared Assembly

Imagine a scenario in which we need to create a general-purpose DLL which can be consumed by different projects or applications. Now, instead of placing it in a particular application, we place the assembly in the global assembly cache. These types of assemblies are called a shared assembly.

Meanwhile, a global assembly cache is just a disk folder where all the shared assemblies are kept. GAC is a central location for all the repositories required by the application.

How to Create an Assembly in .NET

In this section, we will see how assemblies are created. For simplicity, we will create a single file assembly. A single file assembly can be loaded into a single application and cannot be referenced with other assemblies. Here is the example .NET code:

public class AssemblyExample
{
private int total;
public AssemblyExample(){
total = 0;
}
public void AddNumbers(int num){
total+= num;
}
public int GetSum(){
return total;
}
}

The AssemblyExample class here is adding the numbers using AddNumbers method and returning the sum by using the GetSum method.

Now we create another class:

public class AssemblyTypeExample
{
public static int AssemblySumInteger(int[] args){
AssemblyExample e1 = new AssemblyExample();
foreach(int num in args)
{
e1.AddNumbers(num);
}
return e1.GetSum();
}
}

Next, we save both of the files with the names AssemblyExample.cs and AssemblyTypeExample.cs, respectively.

How to Create a Single File Assembly in C#

Next, we will see how C# allows you to create a single file assembly. The generated file assembly will contain both the assembly metadata and the MSIL code. The following command is used to create a single file assembly:

csc /out: SingleFileAssembly.dll /target: library AssemblyExample.cs AssemblyTypeExample.cs

The above statement creates a single file assembly with the name SingleFileAssembly. The /out argument here is used to specify the name of the output file and the /target argument allows you to choose the library assembly files.

Read: 8 .NET Technologies for 2022

What are the Features of .NET Assembly?

The following are some of the features, attributes, and responsibilities of a .NET assembly:

  • Tracking the version dependencies
  • Installing an assembly is much easier than copying the file that contains the assembly
  • Assemblies can be shared or private
  • All of the information of an assembly is packed-up into the assembly itself

Structure of an Assembly in .NET

Four components that make up an assembly are:

  • Assembly Metadata
  • Microsoft Intermediate Language (MSIL)
  • Type Metadata
  • Resources

What is Assembly Metadata?

The information about the assembly is called assembly metadata. It is also known as an assembly manifest. Assembly metadata contains the following information:

  • The assembly’s name
  • Version number
  • Digital signature that uniquely identifies an assembly’s creator.
  • All files which build up the assembly
  • Information regarding all of the referenced assemblies.
  • Information of all the exported classes, methods, properties, and other items.

What is Microsoft Intermediate Language (MSIL)?

MSIL plays a key role in making the .NET a language-neutral technology. Whenever you run the .NET application, the code is compiled down to the same MSIL, no matter what programming language is used. It is the MSIL that makes .NET capable of running independently. With the magic of MSIL, it becomes possible to enable programmers to “write once, run anywhere”.

What is Type Metadata?

The type of an assembly is described by its type metadata. The resources and types that are publicly accessible are also called type metadata. The items, such as image files and other items required by the application, are referred to as resources. In the next section, we will get familiar with the different resources an assembly can have.

Resources

Resources in .NET assembly can be easily placed from the assembly itself or an external resource. The process of accessing resources within an assembly is very straightforward. There are some important classes for dealing with the resources within an assembly. They are listed below:

  • ResourceManager: A program within an assembly used to access and manage available resources.
  • ResourceWriter: A program to write resource information to an external source.
  • ResourceReader: A program within an assembly that reads the material from different resources, including the Internet.

Final Thoughts on Assemblies in .NET

In this .NET programming tutorial, developers learned how .NET compiles our source code into an assembly. The name of the assembly is the same as that of the project name. Please note that, to use an assembly in your application, you have to reference that assembly in your application. Once you reference it in your application, all the classes, methods, properties, and types will become available to your source code just as if they were part of the source code.

Read more .NET programming and software development tutorials.

Tariq Siddiqui
Tariq Siddiqui
A graduate in MS Computer Applications and a Web Developer from India with diverse skills across multiple Web development technologies. Enjoys writing about any tech topic, including programming, algorithms and cloud computing. Traveling and playing video games are the hobbies that interest me most.

More by Author

Must Read