The----of the Sunwen Tutorial C # advanced 5

Source: Internet
Author: User
Tags command line reference
Tutorial Sunwen Tutorial----C # Advanced
Five
Mrfat@china.com
Hello everybody, I am Wuhan Hua Division's Sunwen. I'm back. It is May 2 3:20. I just came back from Computer city and bought a version of MP3. Woo, Hubei students have gone back, especially in Wuhan, run very fast, really hateful. I'm a lone Hokkien, nothing to do, I had to listen to music alone. Fortunately, the music is still better, hehe, in fact, Sunwen a little music cells did not.

Now, I'm going to talk about the library (libraries) and learn how to build a DLL file in C #. Talking about the DLL, is sure that no one knows, no one does not know, this Windows representative, but also is often the object of the attack. Oh, anyway, To learn or to learn. Here we begin, how to compile a C # program into a DLL using a command-line method, and how to use it on the client.

This example consists of two files, one is Factorial.cs, and the function is to compute the factorial of a number. Another is the DigitCounter.cs, which computes the number of numbers in the string argument passed.

We can build the library this way, and do it in the command-line mode:
Csc/target:library/out:functions.dll Factorial.cs DigitCounter.cs

Here's how to use each parameter:

/target:library: Indicates to the system that the output is a DLL library, not an EXE executable file.
/out:functions.dll: Specifies the file name of the DLL for output, that is, Functions.dll, generally, if you omit the first argument, the default file name is the filename of the first file, that is, Factorial.dll.

Next we will build a file, even with this library file, called the client file, FunctionClient.cs. After establishment, compile with the following language name:

Csc/out:functiontest.exe/r:functions.dll FunctionClient.cs

Here's how to use this compilation statement:

/out:functiontest.exe: Indicates that the file name of the output is FunctionTest.exe
/r:functions.dll: Indicates the library to reference, and if it is not in the current directory, you must indicate its full path.


Below I will write the code of these several files in the following:

M://Libraries\factorial.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 for Factorial.cs this file. In line 003, namespace means namespace, according to m$, the library must be packaged according to its name space to make it. NET can load your class correctly.

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

M://Libraries\digitcounter.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 in the same library. The NumberOfDigits method calculates the number of digits in the argument.

The third file is FunctionClient.cs.


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

M://Libraries\functionclient.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 indicates the class of reference Functions.DLL.

If we type the following command at 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 [ten] is [2]

The factorial for [ten] is [3628800]

Note: When you run this. EXE file, it can refer to the DLL file in the current directory, subdirectory, or corpath this environment variable. Corpath This environment variable is the classpath in the. NET environment, which directs the system to look for classes. Plainly, is the Java in the classpath, understand it, Oh.

Well, another one, today's task is done, you can rest. Oh, what's so funny?

Next page


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.