Understanding C# Assembly Types: Private vs. Shared Assemblies
Learn the key differences between private and shared assemblies in C#. This tutorial explains their purposes, deployment strategies, strong names, and the Global Assembly Cache (GAC), helping you choose the appropriate assembly type for your .NET applications.
Understanding Assembly Types in C#
In C#, an assembly is a collection of code and resources compiled into a single unit (an executable file or a DLL). There are several types of assemblies, each serving a different purpose.
Private Assemblies
A private assembly is used exclusively by a single application. It's typically located in the application's directory or a subdirectory. Private assemblies are easy to deploy because they don't require any special registration or installation.
They are created during application compilation and are automatically loaded by the .NET runtime when the application starts.
Shared Assemblies
A shared assembly is designed to be used by multiple applications. It's typically stored in the Global Assembly Cache (GAC) or a shared directory. Shared assemblies require a strong name (a digital signature) to ensure authenticity and version control.
The GAC (located at `C:\Windows\assembly`) is a central repository for shared assemblies. Registering an assembly in the GAC requires using tools like `gacutil.exe`.
Satellite Assemblies
Satellite assemblies store localized resources (e.g., strings, images) for different languages or cultures. They're typically located in subdirectories of the application's directory or the shared assembly's directory.
They follow a specific naming convention (e.g., `MyAssembly.fr.resources.dll` for French resources). The .NET runtime automatically loads the appropriate satellite assembly based on the user's system settings.
Dynamic Link Libraries (DLLs)
A DLL (Dynamic Link Library) is a type of assembly containing code reusable by multiple applications. DLLs are similar to shared assemblies but don't require a strong name. They are typically placed in the application's directory or a subdirectory.
Summary of Assembly Types
Assembly Type | Description | Location | Deployment |
---|---|---|---|
Private | Used by a single application. | Application directory. | Simple; no special registration. |
Shared | Used by multiple applications. | GAC or shared directory. | Requires strong name and GAC registration. |
Satellite | Stores localized resources. | Subdirectory of application or shared assembly. | Automatic loading by .NET runtime. |
DLL | Reusable code for multiple applications. | Application directory or subdirectory. | Simple; no strong name required. |