Home  >  Article  >  Backend Development  >  SUNWEN tutorial - C# advanced (5)

SUNWEN tutorial - C# advanced (5)

黄舟
黄舟Original
2016-12-19 10:20:281076browse

What I’m going to talk about now is libraries, and let’s learn how to use C# to create a DLL file. Speaking of DLL, everyone must know it. This is a typical representative of WINDOWS, and it is often everyone’s favorite. The target of attack. Haha, no matter what, you still have to learn. Let’s start with how to compile a C# program into a DLL using the command line and how to use it on the client.

This example includes two There are two files, one is Factorial.cs, which is used to calculate the factorial of a number. The other is DigitCounter.cs, which is used to calculate the number of numbers in the passed string parameter.

We can build the library like this, in Do this in command line mode:
csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs

Let’s talk about the usage of each parameter:

/target:library: Indicate to the system that the output is a DLL library, rather than an EXE executable file.
/out: Functions.dll: Specify the file name of the output DLL, namely Functions.dll. Generally, if you omit the first parameter, the default file name will be The file name of the first file is Factorial.dll.

Next we will create another file, that is, the file using this library, called the client file, FunctionClient.cs. After it is created, compile it with the following language name:

csc /out:FunctionTest.exe /R:Functions.DLL FunctionClient.cs

Let’s talk about the usage of this compilation statement:

/out:FunctionTest.exe: Indicate that the output file name is FunctionTest.exe
/R: Functions.DLL: Point out the library to be referenced. If it is not in the current directory, its full path must be pointed out.


I will write the code of these files below:

000: // LibrariesFactorial. cs
001: using System;
002:
003: namespace Functions
004: {
005: public class Factorial
006: {
007: public static int Calc(int i)
008: {
009: return(( i <= 1) ? 1 : (i * Calc(i-1)));
010: }
011: }
012: }
This is the code of the Factorial.cs file. In line 003, namespace means namespace. According to M$, the library must be packaged according to its namespace so that .NET can load your class correctly.

The following is the content of the DigitCounter.cs file:

000: // LibrariesDigitCounter.cs
001: using System;
002:
003: namespace Functions
004: {
005: public class DigitCount
006: {
007: public static int NumberOfDigits(string theString)
008: {
009 : int count = 0;
010: for ( int i = 0; i < theString.Length; i++ )
011: {
012: if ( Char.IsDigit(theString[i]) )
013: {
014 : count++;
015: }
016: }
017:
018: return count;
019: }
020: }
021: }
Note that the namespace in this example should be consistent with the first one, because they It is the NumberOfDigits method in the same library that calculates the number of numbers in the parameters.

The third file is FunctionClient.cs


We know that once a library is created, it can be used by other classes (nonsense, Otherwise, how can we call it a library?). The following C# program uses the classes in the library we just created.

000: // LibrariesFunctionClient.cs
001: using System;
002: using Functions;
003: class FunctionClient
004: {
005: public static void Main(string[] args)
006: {
007: Console.WriteLine("Function Client");
008:
009: if ( args.Length == 0 )
010: {
011: Console.WriteLine("Usage: FunctionTest ... ");
012: return;
013: }
014:
015: for ( int i = 0; i < args.Length; i++ )
016: {
017: int num = Int32.Parse(args[i]);
018: Console.WriteLine(
019: "The Digit Count for String [{0}] is [{1}]" ,
020: args[i],
021: DigitCount.NumberOfDigits(args[i]));
022: Console.WriteLine(
023: "The Factorial for [{0}] is [{1}]",
024: num,
025: Factorial.Calc(num) );
026: }
027: }
028: }
In line 002, a using Functions specifies a reference to the Functions.DLL class.

If we Type the following command on the command line and you can see the output:


FunctionTest 3 5 10

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: When you run this .EXE file, the DLL file it refers to can be in the current directory, a subdirectory, or the CORPATH environment variable. The CORPATH environment variable is in the .NET environment The class path in is used to guide the system to find classes. To put it bluntly, it is CLASSPATH in java. You understand, haha.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn