0% found this document useful (0 votes)
31 views5 pages

Assemblie in C#

Assemblies are essential components of .NET applications that encapsulate code and resources in a reusable format, supporting versioning and security. They can be categorized into private, shared, and satellite assemblies, and consist of a manifest, metadata, Intermediate Language (IL) code, and resources. Best practices for using assemblies include strong naming, version management, documentation, testing, and security measures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Assemblie in C#

Assemblies are essential components of .NET applications that encapsulate code and resources in a reusable format, supporting versioning and security. They can be categorized into private, shared, and satellite assemblies, and consist of a manifest, metadata, Intermediate Language (IL) code, and resources. Best practices for using assemblies include strong naming, version management, documentation, testing, and security measures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assemblies 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.

Visual Programming Notes – C#


Assemblies in c#
Creating an Assembly
Creating an assembly in C# involves writing code, compiling it, and optionally defining an
assembly manifest and adding resources. Here’s a simple example to illustrate this process:
Step 1: Write the Code
Create a simple C# class in a file named MathOperations.cs.
using System;
namespace MyLibrary
{
public class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}

public int Multiply(int a, int b)


{
return a * b;
}
}
}
Step 2: Compile the Code
Use the .NET CLI to compile the code into an assembly.
sh code
dotnet new classlib -n MyLibrary
cd MyLibrary
dotnet add package System.Console
dotnet build
This creates a DLL file named MyLibrary.dll in the bin/Debug/netstandard2.0 directory.
Step 3: Create a Console Application to Use the Assembly

Visual Programming Notes – C#


Assemblies in c#
Create a new console application and reference the MyLibrary assembly.
sh code
cd ..
dotnet new console -n MyApp
cd MyApp
dotnet add reference ../MyLibrary/MyLibrary.csproj
Edit the Program.cs file in the MyApp project to use the MyLibrary assembly.
using System;
using MyLibrary;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
MathOperations math = new MathOperations();
int sum = math.Add(5, 10);
int product = math.Multiply(5, 10);

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

Visual Programming Notes – C#


Assemblies in c#
Sum: 15
Product: 50
Assembly Versioning
Assemblies have version numbers that consist of four parts:
• Major: Major changes, potentially breaking compatibility.
• Minor: Minor changes, typically backward compatible.
• Build: Incremental build number.
• Revision: Fixes or small improvements.
You can specify the version number in the assembly manifest using attributes:
csharp code
using System.Reflection;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Global Assembly Cache (GAC)
The GAC is a machine-wide cache used to store assemblies intended to be shared across
multiple applications. To install an assembly in the GAC, use the gacutil tool:
sh code
gacutil -i MyLibrary.dll
Assemblies in the GAC must have a strong name, which involves creating a key pair and
signing the assembly:
1. Generate a Key Pair:
sh code
sn -k MyKey.snk
2. Sign the Assembly:
Add the following attribute to the AssemblyInfo.cs file:

[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.

Visual Programming Notes – C#


Assemblies in c#
3. Documentation: Document your assemblies to make them easier to understand and
use.
4. Testing: Implement comprehensive tests to ensure the reliability of your assemblies.
5. Security: Follow best practices for code security, including code signing and obfuscation
if necessary.
By understanding and utilizing assemblies effectively, you can build modular, reusable, and
maintainable applications in .NET.

Visual Programming Notes – C#

You might also like