Library in C)
To put it bluntly, I want to talk about libraries. I want to work with you to learn how to use C # To create a DLL file. when talking about DLL, it must be a typical representative of WINDOWS, and it is often the target of everyone's efforts. well, no matter what you do, learn or learn. let's start with how to use the command line to compile a C # program into a DLL and how to use it on the client.
This example contains two files: Factorial. cs, used to calculate the Factorial of a number, and DigitCounter. cs, used to calculate the number of numbers in the passed string parameter.
We can create a database in this way, in the command line mode:
Csc/target: library/out: Functions. dll Factorial. cs DigitCounter. cs
The following describes the usage of each parameter:
/Target: library: indicates to the system that the output is a DLL library, rather than an EXE executable file.
/Out: Functions. dll: Specifies the name of the output DLL, that is, Functions. dll. Generally, If you omit the first parameter, the default file name is the name of the first file, that is, Factorial. dll.
Next we will create a file, that is, the file using this library, called the client file, FunctionClient. cs. After the file is created, compile it with the following language name:
Csc/out: FunctionTest.exe/R: Functions. DLL FunctionClient. cs
The usage of this compilation statement is as follows:
/Out: FunctionTest.exe: indicates that the output file name is FunctionTest.exe.
/R: Functions. DLL: Specifies the library to be referenced. If it is not in the current directory, you must specify its full path.
Below I will write the code for 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 Factorial. cs file code. in row 003, namespace indicates the namespace. According to M $, the library must be packaged according to its namespace to make. NET can load your class correctly.
The contents of the DigitCounter. cs file are as follows:
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 are the. NumberOfDigits method in the same library, and the number of numbers in the parameter is calculated.
The third file is FunctionClient. cs.
We know that once a database is created, it can be used by other classes (nonsense, or how can it be called a database ?). 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 row 002, A using Functions specifies the reference Functions. DLL class.
If we type the following command in the command line, we 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. when the EXE file is used, it can reference the DLL file in the current directory, subdirectory, or CORPATH environment variable. the environment variable CORPATH is in.. NET environment, which is used to guide the system in searching for classes. to put it bluntly, it's the CLASSPATH in JAVA.
Now, I have finished another article.