C# > Compiler and Runtime > C# Compilation Process > Assemblies and Modules
Simple Assembly Example
This example demonstrates the creation and usage of a simple assembly in C#. It illustrates how code is organized into assemblies and how different parts of an application can interact through them.
Defining a Class in an Assembly
This code defines a class named MyClass
within the MyNamespace
namespace. This class is intended to be part of an assembly named 'MyAssembly'. The GetMessage()
method simply returns a string.
// MyAssembly.cs
using System;
namespace MyNamespace
{
public class MyClass
{
public string GetMessage()
{
return "Hello from MyAssembly!";
}
}
}
Compiling the Code into an Assembly
This command compiles the MyAssembly.cs
file into a dynamic link library (DLL) file, which represents the assembly. The /target:library
option tells the C# compiler to create a DLL instead of an executable.
// Compile using the command line:
// csc /target:library MyAssembly.cs
Referencing and Using the Assembly
This code demonstrates how to reference and use the MyAssembly
assembly. The using MyNamespace;
statement allows access to the classes within the MyNamespace
namespace defined in the assembly. An instance of MyClass
is created, and its GetMessage()
method is called, displaying the message on the console.
// MainProgram.cs
using System;
using MyNamespace;
class MainProgram
{
static void Main(string[] args)
{
MyClass myObject = new MyClass();
string message = myObject.GetMessage();
Console.WriteLine(message);
}
}
Compiling the Main Program
This command compiles the MainProgram.cs
file. The /reference:MyAssembly.dll
option tells the C# compiler to include the MyAssembly.dll
assembly as a reference during compilation. This allows the MainProgram
to use the classes defined in MyAssembly
.
// Compile using the command line:
// csc /reference:MyAssembly.dll MainProgram.cs
Concepts Behind the Snippet
This snippet illustrates the basic concepts of assemblies and modules in C#:
Real-Life Use Case
Imagine you are developing a large application with multiple features. You can divide the application into separate assemblies, each responsible for a specific feature. For example, you could have an assembly for handling user authentication, another for managing database access, and a third for providing reporting functionality. This modular approach makes the application easier to maintain, test, and update.
Best Practices
Interview Tip
Be prepared to explain the difference between assemblies and modules. An assembly is a logical grouping of code, while a module is a physical file containing compiled code. An assembly can consist of one or more modules.
When to use them
Use assemblies to modularize your code, promote reusability, and simplify deployment. They are particularly useful for large projects or when you want to share code between multiple applications.
Alternatives
Alternatives to assemblies for code organization include using multiple projects within a single solution or relying on source code sharing. However, assemblies offer better encapsulation, versioning, and deployment capabilities.
Pros
Cons
FAQ
-
What is the difference between an assembly and a module?
An assembly is a logical grouping of one or more modules (typically DLL or EXE files), while a module is a physical file containing compiled code. An assembly provides a unit of deployment, versioning, and security. -
What is a strong name?
A strong name is a digital signature that uniquely identifies an assembly. It includes the assembly's name, version, culture, and a public key. Strong names help prevent naming conflicts and ensure that the assembly has not been tampered with.