What Is An Assembly In C#?

C Sharp Assembly

C# is a compiled language. This means that when a developer creates C# source files, those files need to be compiled before they can run. You can not write a C# file, and then run that file or combination of files like you can with say a JavaScript or PHP file. This is where assemblies come into play for a compiled language. Assembly files are the result of the C# compiler reading in human readable C# source files, and then producing a file the application can use. In fact a C# program will often consist of many assembly files working together.

Compiling A C# Source File

You actually don’t need a fancy IDE to write and compile a C# program. Of course it is many times easier since something like Visual Studio will do a lot of the heavy lifting for you. Let’s see how to use the C# compiler right from the command line using the csc.exe program.
c sharp compiler csc

In the command line, we can use notepad.exe to create a simple keto.cs program file. We’ll make a program that simply outputs, “I Like Keto”. Check out this simple program here. We don’t get any syntax highlighting or help at all like with Visual Studio.
csharp file in notepad

Now, we want to compile this source code file right from the command line. Well csc.exe is the program we want to use and it lives at C:WindowsMicrosoft.NETFramework>v4.0.30319csc as seen here.
microsoft csc compiler location

To compile our C Sharp source file, we can simply call the csc.exe program from the command line and pass in the name of the file that we want to compile. Here we compile the keto.cs file into a keto.exe file.
compile csharp file at command line example

Cool! The compiler has created an executable file that can now be run from the command line. In fact, we just created our very first Assembly in C#! Yes, it is an exe file, but technically it can be referred to as an Assembly. A .NET assembly is a file that contains compiled code which can now execute within the Common Language Runtime. Here is our assembly in all of its glory.

.NET assemblies are files which have either a .exe or a .dll extension. The first type is a file that is executable, like we just saw above. Dll files are a little bit different. A dll file means it is what is known as a dynamic link library. Dll files are not executed like an exe file, but they can be loaded by other programs and run the code contained in them. These types of files are used often for code reuse. The .NET Framework is a great example of code reuse. Common classes like Console and DateTime in .NET live inside an assembly the .NET framework provides named mscorlib.dll.

The Global Assembly Cache

The .NET assemblies are part of the Global Assembly Cache on a Windows computer. The Global Assembly Cache is located at the C:WindowsassemblyGAC> directly like we see here.
Windows Global Assembly Cache

The assemblies stored in the GAC can be used by any .NET applications running on the computer. Common assemblies like mscorlib and others live in the GAC making one copy available for any applications that need it on the computer.

Working With Assemblies In Visual Studio

build visual studio files into an assembly

When building a solution in Visual Studio, what is happening is all of the source code files are collected together and built into an assembly. In this scenario it is being built into an executable assembly, or a file with a .exe extension. This file gets created by the C# compiler in the bin/Debug directory. Let’s take a peek:

 Volume in drive C is OSDisk
 Volume Serial Number is F232-B880

 Directory of C:UsersusersourcereposStocksStocksbinDebug

02/05/2019  01:43 PM              .
02/05/2019  01:43 PM              ..
02/06/2019  05:29 PM             5,632 Stocks.exe
02/04/2019  04:46 PM               189 Stocks.exe.config
02/06/2019  05:29 PM            22,016 Stocks.pdb
               3 File(s)         27,837 bytes
               2 Dir(s)  83,911,729,152 bytes free

We can see that Stocks.exe is a file that the C# compiler created. It is an assembly which can be run because of it’s .exe extension. The reason for this is because when we created this project, we chose to create a Console Application with the name of Stocks. Visual Studio has a bunch of options that get configured based on the type of project you choose. The assembly name is the same as the project name, so in this case it is Stocks.

The extension given to the assembly is controlled by the Output type setting. For either the Windows Application or Console Application, we will get that .exe extension. If we choose the Class Library option, the assembly would not longer get an .exe extension but instead a .dll extension.
vs application output type menu

Just for grins, we can change the output type to Class Library and then build the solution again. Now when we look at the directory contents in the Debug folder, sure enough we see Stocks.dll instead of Stocks.exe.

 Volume in drive C is OSDisk
 Volume Serial Number is F232-B880

 Directory of C:UsersusersourcereposStocksStocksbinDebug

02/08/2019  04:53 PM              .
02/08/2019  04:53 PM              ..
02/08/2019  04:53 PM             5,120 Stocks.dll
02/04/2019  04:46 PM               189 Stocks.dll.config
02/08/2019  04:53 PM            19,968 Stocks.pdb
               3 File(s)         25,277 bytes
               2 Dir(s)  83,904,868,352 bytes free

This type of file can not be run directly. In fact if we try to do so we get a message from Visual Studio saying, “A project with an Output Type of Clas Library cannot be started directly. In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.”
choose correct output type in visual studio

Go To Assembly Definition

A neat way to learn more about an assembly you are interested in is to right-click on the assembly name in Visual Studio, and then choose “Go To Definition”. Here, you can see we right-click on the Console assembly and then Go To Definition.
visual studio go to definition

Visual Studio will then open a new window with an outline of all the available methods in that class. This is not the source code, but almost like a collection of interfaces. The file shows you what you can invoke from the Console class, much like when you’re typing and intellisense tries to guess what method you want to use. Notice at the very top of the file we can see what assembly it is using.

#region Assembly mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1mscorlib.dll

mscorlib assembly

Viewing Project Assemblies

To have a look at the assemblies that are used by a particular project in Visual Studio, you can look in the Solution Explorer, and then click the References option.
visual studio solution explorer references

Listed under this node are things like Microsoft.CSharp, System, System.Core, System.Data, System.Data.DataSetExtensions, System.Net.Http, System.xml, and System.Xml.Linq. These are other assemblies that this project may reference. Many of these are automatically included by Visual Studio when you create a new project as it tries to access to common assemblies that many applications use. Even if you don’t use some of the assemblies seen in the References node, it’s not a problem since .NET will only load an assembly when you actually use a class from that assembly inside of a program.

Looking Closer With The Object Browser

To learn more about a particular assembly you can also use the Object Browser to drill down into what’s available. For example, let’s look at System.Net.Http in the object browser.
view in object browser

With this, System.Net.Http opens in the Object Browser and we can see all of the functionality available in that assembly. We can see several classes such as HttpClient, HttpClientHandler, HttpCompletionOption, HttpContent, and so on. Of course inside each of these classes are properties and methods. Inspecting that HttpMethod class shows several properties such as Delete, Get, Head, Method, Options, Post, Put, and Trace. Then if we are interested for example in making GET requests we can drill down on HttpMethod, then choose Get, and notice the information we are provided.

public static System.Net.Http.HttpMethod Get { get; }
    Member of System.Net.Http.HttpMethod

Represents an HTTP GET protocol method.

Returns System.Net.Http.HttpMethod.

System-Net-Http in Object Browser

Adding References To Your Project

visual studio add reference There are so many assemblies available in the .NET Framework, it’s almost impossible to know them all. Chances are, you won’t need to create much from scratch as most common problems have already been solved by one of the available assemblies. For example, say you want to add support for Charts in you application. Go ahead and add a reference to the System.Web.DataVisualization assembly.
Reference Manager Web Data Visualization

This will give you access to the System.Web.UI.DataVisualization.Charting, System.Web.UI.DataVisualization.Charting.Borders3D, System.Web.UI.DataVisualization.Charting.ChartTypes, System.Web.UI.DataVisualization.Charting.Data, System.Web.UI.DataVisualization.Charting.Formulas, and System.Web.UI.DataVisualization.Charting.Utilities namespaces which collectively hundreds and hundreds of methods and properties dealing with creating and rendering any type of chart you might need such as bar charts, pie charts, and various custom charts.

What Is An Assembly In C# Summary

In this tutorial, we learned how Visual Studio and the C# compiler will compile our source code into an assembly. The assembly name will have the same name as the project. The resulting assembly will have an .exe or .dll extension depending on if you were building a program to execute directly, or a class library. We also saw that more than one assembly is required for most applications, and we learned how to add references to other assemblies. In .NET all the types a developer creates will live in an assembly.