Assemblie in C#
Assemblie in C#
Assemblies in C#
Assemblies are the building blocks of .NET applications. They encapsulate code and resources
in a portable, versionable, and secure format, which can be reused across multiple applications.
Understanding assemblies is crucial for effective .NET development, as they play a key role in
application deployment, versioning, and security.
What is an Assembly?
An assembly is a compiled code library used by .NET applications. It can contain one or more
managed code modules (e.g., DLLs, EXEs), as well as metadata about the types defined within
those modules and the resources (e.g., images, strings) used by those types.
Assemblies serve several key functions:
1. Code Encapsulation: Assemblies encapsulate code and resources, making them
reusable and distributable.
2. Versioning: Assemblies can have version numbers, allowing multiple versions of the
same assembly to coexist.
3. Security: Assemblies contain metadata that supports code security mechanisms, such
as code access security.
4. Type Resolution: The metadata within assemblies helps the .NET runtime locate and
load types at runtime.
Types of Assemblies
1. Private Assemblies: These are intended for use by a single application and are
typically stored in the application's directory.
2. Shared Assemblies: These are designed to be shared across multiple applications and
are usually installed in the Global Assembly Cache (GAC).
3. Satellite Assemblies: These are used to deploy language-specific resources for an
application. They enable localization of applications by containing resources for a
specific culture or language.
Assembly Structure
An assembly consists of the following components:
1. Manifest: Contains metadata about the assembly, including its name, version, culture,
and the list of files it contains.
2. Metadata: Information about the types, members, and references in the assembly. This
metadata is used by the .NET runtime for type resolution, security, and other purposes.
3. Intermediate Language (IL) Code: The actual code that is executed by the .NET
runtime. This code is compiled from source code and is platform-independent.
4. Resources: Additional data required by the assembly, such as images, strings, and
other non-code elements.
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Product: {product}");
}
}
}
Build and run the application:
sh code
dotnet build
dotnet run
The output should be:
makefile code
[assembly: AssemblyKeyFile("..\\..\\MyKey.snk")]
Best Practices
1. Strong Naming: Use strong naming to uniquely identify your assemblies.
2. Version Management: Follow semantic versioning to manage assembly versions
effectively.