What Exactly Is an Assembly in C# or .Net

What exactly is an Assembly in C# or .NET?

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.

What are .NET Assemblies?

In more simple terms: A chunk of (precompiled) code that can be executed by the .NET runtime environment. A .NET program consists of one or more assemblies.

Difference Between Assembly and DLL

An assembly is .NET's "minimum unit of deployment". Usually an assembly corresponds to a single file, but it doesn't have to - you can have multiple files, with one of them being the master which knows where all the other bits are.

Single-file assemblies are usually DLLs or EXE files. If you've got a normal class library and you just want to send it to the other side, the DLL is what you want. I'd only worry about more complicated scenarios as and when you run into them :)

what is the need of assembly ? why we use them?

To quote the MSDN article on assemblies:
"Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality."

An assembly in .NET is a unit of code that has been compiled together into a single executable, library, or module. Whenever you compile code, you will generate an assembly. I do not believe there is a way to use .NET code without using an assembly.

You can use reflection to learn about the types in an assembly as well as other metadata.

C# assemblies, whats in an assembly?

Namespaces affect name resolution only. Namespaces do not imply any sort of storage, nor do namespaces determine which DLLs contain your code. Namespaces allow you to group related things together under a logical name even though they may physically reside in different DLLs.

An assembly is basically just a DLL or EXE file. It contains IL code and type information that describes the code in that DLL or EXE. It can contain a lot of other stuff too, but for starters just think of it as a DLL.

You put your code into a particular assembly by compiling your code into a project (csproj) that produces the DLL or EXE.

A namespace can span multiple assemblies. That is, classes that are members of that logical namespace may reside in multiple DLLs. You can access a particular class in your source code only if your project references the correct assembly (DLL) that contains that class.

The Internal modifier means that the symbol can only be accessed from within the same assembly. Only code that is compiled into the same DLL as your code can access your properties or methods that are tagged with internal.

How to know what assembly to add C#

If it is a .NET framework function, you can just search it on MSDN, and it will tell you in which assembly the class/function exists.

You can also use ReSharper which is a very nice plugin to Visual Studio, and it can help you add assemblies automatically.



Related Topics



Leave a reply



Submit