156

Could you please explain what is an Assembly in C# or .NET?

  1. Where does it begin and where does it end?
  2. What important information should I know about Assemblies?
starblue
  • 55,348
  • 14
  • 97
  • 151
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
  • 8
    Strictly speaking, isn't an assembly a .NET concept rather than a C# concept? – JeffH Sep 01 '09 at 13:43
  • 19
    @JeffH: when I asked the question I thought it was a C# concept. Now I understand it's a .NET one. Nevertheless, since I think other people may be confused like I was, I intentionally did not change it for Google to accommodate for this mistake... – Roee Adler Sep 01 '09 at 13:48
  • 1
    It's a C# concept as well. You have to understand it to use "internal" access modifier properly. – Jan Rozycki Oct 21 '20 at 10:38
  • My grain of salt: https://stackoverflow.com/a/54803483/7389293 – carloswm85 Dec 31 '21 at 14:30

9 Answers9

161

An assembly is the compiled output of your code, typically a DLL, but your EXE is also an assembly. It's the smallest unit of deployment for any .NET project.

The assembly typically contains .NET code in MSIL (Microsoft Intermediate language) that will be compiled to native code ("JITted" - compiled by the Just-In-Time compiler) the first time it is executed on a given machine. That compiled code will also be stored in the assembly and reused on subsequent calls.

The assembly can also contain resources like icons, bitmaps, string tables and so on. Furthermore, the assembly also contains metadata in the assembly manifest - information like version number, strong name, culture, referenced assemblies and so forth.

In 99% of your cases, one assembly equals a physical file on disk - the case of a multi-file assembly (one assembly, distributed across more than a single file) appears to be a rather odd-ball edge case which I've never encountered so far in my 5+ years of .NET development.

In a multifile assembly there would still be only one assembly manifest in a DLL or EXE and the MSIL code in multiple netmodule files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    @marc_s: I think you mean a multi-assembly file. It's possible to package multiple assemblies within a single file via studio's command-line tools, but not directly via the IDE. – Greg D Sep 01 '09 at 13:15
  • 6
    A very important thing in an assembly is the metadata in the assembly manifest. The manifest has information like version, strong name, culture, referenced assemblies etc. In a multifile assembly there would still be only one assembly manifest in a dll or exe and the MSIL code in multiple .netmodule files. Basically an assembly is minimum unit of deployment in >net – softveda Sep 01 '09 at 13:34
  • @marc_s: I would appreciate it if you add @Pratik's point to the answer, thanks – Roee Adler Sep 01 '09 at 13:43
  • @rax: done; @pratik: now you're confusing me: you talk about "multi-file assembly" again, while @GregD mentioned it really was a "multi-assembly file" - what is it now?? (as I said - I've never encountered such a beast, so I'm a bit unclear whether it's multiple assemblies per file, or multiple files per assembly....) – marc_s Sep 01 '09 at 14:04
  • @GregD: it seems to be "multi-file assembly" after all - check out http://msdn.microsoft.com/en-us/library/226t7yxe.aspx – marc_s Sep 01 '09 at 14:05
  • 3
    "That compiled code will also be stored in the assembly and reused on subsequent calls." This is misleading and/or not very clear. No compiled code is "stored" in an assembly. I think you may be referring to the way each method/function is "jitted" to native code when it is first called. Then, while the assembly is in memory, the native code version of the method is called. If the assembly is unloaded, and later reloaded, the jit process will occur all over again. Of course this assumes that you are not using NGEN to pre-compile your assemblies to native code (not recommended). – Ash Sep 07 '09 at 11:58
19

The answer is in order for immediate-grasping.

Put simply, it is the compiled project involving your classes and additional files, if there are. That is, each project in a solution is assembly.

Or more techinally,

An assembly is where a type is stored in the flesystem. Assemblies are a mechanism for deploying code. For example, the System.Data.dll assembly contains types for managing data. To use types in other assemblies, they must be referenced. - Source

How do we know it? If you glance at properties of a project under the solution you can see the following images.

When you compile the project, it turns out to DLL or EXE.

enter image description here enter image description here enter image description here

  • Is there a way to locate the actual assembly file from within the IDE? I know it can be located from the Windows Explorer, in the bin folder inside the project, but this is an unpractical way of finding the files. – carloswm85 Dec 31 '21 at 14:39
  • @carloswm85 why don't you simply right click and lovate its folder? https://i.stack.imgur.com/RzsRB.png – Soner from The Ottoman Empire Dec 31 '21 at 17:33
  • not the solution I'm looking for. I don't want to have to access to the Windows Explorer. I found a solution, though. From inside VS19: In `Solution Explorer` click `Show All Files` menu button, then the `bin` folder (and others) will appear. Open `debug` folder, right click on the desired assembly and click `Copy Path`. – carloswm85 Dec 31 '21 at 18:01
14

.NET assembly

In the Microsoft .NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security.

9

http://www.codeguru.com/columns/csharp_learning/article.php/c5845

An assembly is a file that is automatically generated by the compiler upon successful compilation of every .NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated.

Raghav
  • 8,772
  • 6
  • 82
  • 106
4

Here's another explanation of the make up of .NET Assemblies, a mini-quote:

The .NET framework consists of the concepts of modules, assemblies, which both store metadata and manifest information. An assembly can contain multiple modules. Visual C# only ever creates one module which is turned into an assembly by the C# compiler (csc.exe), but an assembly can link many .NET modules together via the assembly linker (al.exe) command-line tool. For example each of your source code .cs files could be compiled into a module and linked together to form an assembly - an assembly is just a collection of modules and resources. One of these modules, however; must contain manifest metadata (see below) information for the assembly to be understood by the CLR.
....
Having created a new .exe or .dll inside VS.NET you see your file appear inside your bin folder. Opening it in notepad will give out gibberish, or even inside a hexadecimal editor without knowing the structure of the file, you need a tool like ildasm.exe or CFF explorer to make meaning from it. The structure of the assembly is as follows:

PE header
CLR header
CLR metadata
CLR
IL code
Native data

Community
  • 1
  • 1
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • 1
    Where the hell is that link taking me to? Kind of infinite redirects! After a few redirects it took me to a page that says [this](https://m.imgur.com/a/7g1TrTD). No way to get original the URL you posted either. – Sнаđошƒаӽ Oct 03 '18 at 01:42
1

When a source code is compiled by the language compiler it Generate a Managed Assembly and MSIL(MisroSoft Intermediate Language). That Assembly contains .dll or .exe file. An Assebmly can be of two types Private Assembly and Shared Assembly, shared Assembly is stored in GAC(Global Assembly Cache) so that any application can refer to it while private assembly is stored in application folder which can be used by only one Application.

gaurav patni
  • 45
  • 1
  • 8
0

An assembly is a DLL or an EXE which will be created when you publish it or compile your application.

Anup Shetty
  • 571
  • 8
  • 10
0

After writing source code of your program(project) then a file is created which may be DLL or EXE depends on your project. It makes only once for a single project. It has two types 1:- single 2:- shared or multiprogram single assembly used only in a single program while shared can be used for multiprogram

  • _After writing source code of your program(project) then a file is created which may be DLL or EXE depends on your project_ - after writing the source code? Doesn't seem to be a very good indication as to _when_ that file is created, which BTW is immaterial given the question. – Sнаđошƒаӽ Jul 23 '18 at 18:01
0

An Assembly is a collection of logical units. Logical units refer to the types and resources which are required to build an application and deploy them using the .Net framework. Basically, Assembly is a collection of Exe and DLLs. It is portable and executable.

Abhay.Patil
  • 663
  • 1
  • 6
  • 14