This tutorial shows how to create and use libraries in C#.
Sample Files
See Libraries Sample to download and build the sample files discussed in this tutorial.
Further Reading
- Creating and Using C# DLLs
- C# Compiler Options
Tutorial
This tutorial demonstrates how to create a managed DLL file by using the necessary compiler options, and how to use the library by a client program.
Example
This example uses the following modules:
Building the Library
To build the library, make Functions the current directory and type the following at the command prompt:
csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs
where:
/target:library |
Specifies that the output is a DLL and not an executable file (this also stops the compiler from looking for a default entry point). |
/out:Functions.dll |
Specifies that the output file name is Functions.dll. Normally the output name is the same name as the first C# source file on the command line (in this example, Factorial). |
Factorial.cs DigitCounter.cs |
Specifies the files to compile and place in the DLL. |
Compiling the Client
To compile the program, make FunctionTest the current directory and type the following at the command prompt:
copy ..\Functions\Functions.dll .
csc /out:FunctionTest.exe /R:Functions.DLL FunctionClient.cs
where:
/out:FunctionTest.exe |
Specifies that the output file name is FunctionTest.exe. |
/R:Functions.DLL |
Specifies that Functions.DLL must be included when resolving references. This DLL must be located in the current directory or have a fully qualified path. |
FunctionClient.cs |
Specifies the client source code. |
This creates the executable file FunctionTest.exe.
File 1 - Factorial.cs
The following code calculates the factorial of the integer passed to the method (unlike Libraries Sample, compile this to a library).
// Factorial.cs
// compile with: /target:library
using System;
// Declares a namespace. You need to package your libraries according
// to their namespace so the .NET runtime can correctly load the classes.
namespace Functions
{
public class Factorial
{
// The "Calc" static method calculates the factorial value for the
// specified integer passed in:
public static int Calc(int i)
{
return((i <= 1) ? 1 : (i * Calc(i-1)));
}
}
}
File 2 - DigitCounter.cs
The following code is used to count the number of digit characters in the passed string:
// DigitCounter.cs
// compile with: /target:library /out:Functions.dll /reference:Factorial.dll
using System;
// Declare the same namespace as the one in Factorial.cs. This simply
// allows types to be added to the same namespace.
namespace Functions
{
public class DigitCount
{
// The NumberOfDigits static method calculates the number of
// digit characters in the passed string:
public static int NumberOfDigits(string theString)
{
int count = 0;
for ( int i = 0; i < theString.Length; i++ )
{
if ( Char.IsDigit(theString[i]) )
{
count++;
}
}
return count;
}
}
}
File 3 - FunctionClient.cs
Once you build a library, it can be used by other programs. The following client program uses the classes defined in the library. The basic function of the program is to take each command-line parameter and attempt to compute the factorial value for each argument.
// FunctionClient.cs
// compile with: /reference:Functions.dll,Factorial.dll /out:FunctionTest.exe
// arguments: 3 5 10
using System;
// The following using directive makes the types defined in the Functions
// namespace available in this compilation unit:
using Functions;
class FunctionClient
{
public static void Main(string[] args)
{
Console.WriteLine("Function Client");
if ( args.Length == 0 )
{
Console.WriteLine("Usage: FunctionTest ... ");
return;
}
for ( int i = 0; i < args.Length; i++ )
{
int num = Int32.Parse(args[i]);
Console.WriteLine(
"The Digit Count for String [{0}] is [{1}]",
args[i],
// Invoke the NumberOfDigits static method in the
// DigitCount class:
DigitCount.NumberOfDigits(args[i]));
Console.WriteLine(
"The Factorial for [{0}] is [{1}]",
num,
// Invoke the Calc static method in the Factorial class:
Factorial.Calc(num) );
}
}
}
Output
The command line FunctionTest 3 5 10 uses the program FunctionTest to calculate the factorial of the three integers 3, 5, and 10. It also displays the number of digits for each argument.
This run gives the output:
Function Client
The Digit Count for String [3] is [1]
The Factorial for [3] is [6]
The Digit Count for String [5] is [1]
The Factorial for [5] is [120]
The Digit Count for String [10] is [2]
The Factorial for [10] is [3628800]
Note To run the client executable (FunctionTest.exe), the file Functions.DLL must be in the current directory, a child directory, or in the Global Assembly Cache. For more information see Global Assembly Cache.
See Also
C# Tutorials