Splitting your C♯ Code into Multiple Files 2: DLLs
I have found out about compiling and linking to DLLs. I think this is called Dynamic Linking but again, I could be wrong.
We will be using the example files from last time:
filea.cs:
using System;
class ClassA
{
public static void Main()
{
Console.WriteLine("This is a test from file A");
Someplace.ClassB.PrintHello();
}
}
fileb.cs:
using System;
namespace Someplace
{
public class ClassB
{
public static void PrintHello()
{
Console.WriteLine("Another hello from file B!");
}
}
}
This is a 2 step process. First we need to compile the DLL, then we need to compile the main exe and link to the DLL.
To compile the DLL, you type something like this:
csc /target:library fileb.cs
The important bit here is /target:library. This tells the C♯ compiler to compile your code to a DLL and not and exe.
To compile the main exe, you need to type something like this:
csc /reference:fileb.dll filea.cs
This tells the C♯ compiler to compile the code in filea.cs into an exe, and link it to fileb.dll.
Taken from this MSDN page.