兩個類別檔 (MySwap.cs、MyMaxCD.cs) 置於同一命名空間 MyMethods,便於呼叫端單一 using 引用。
# 進入 .NET Framework 目錄 (含 csc.exe)
csc /target:library /out:MyDLL.dll MySwap.cs MyMaxCD.cs
/target:library:輸出 DLL 而非 EXE。/out:MyDLL.dll:指定輸出檔名 (省略則以第一個原始檔名為預設)。// MyClient.cs
using System;
using MyMethods; // 引用 DLL 命名空間
class MyClient
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: MyClient ");
return;
}
long num1 = long.Parse(args[0]);
long num2 = long.Parse(args[1]);
SwapClass.Swap(ref num1, ref num2);
Console.WriteLine("The result of swap is num1 = {0} and num2 = {1}", num1, num2);
long maxcd = MaxCDClass.MaxCD(num1, num2);
Console.WriteLine("The MaxCD of {0} and {1} is {2}", num1, num2, maxcd);
}
}
csc /out:MyClient.exe /reference:MyDLL.dll MyClient.cs
/reference:MyDLL.dll:指定相依組件,編譯器會讀取其中的中繼資料。MyClient 123 456
輸出應與預期一致 (見下篇)。