|
This tutorial shows how to create and use libraries in C#.
TutorialThis 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. ExampleThis 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:
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:
This creates the executable file File 1 - Factorial.csThe following code calculates the factorial of the integer passed to the method. // 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.csThe 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.csOnce 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) );
}
}
}
OutputThe command line 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] See Also |
