C # simple compilation-CSC command Application

Source: Internet
Author: User

[It168 Knowledge Base]
First, you must install the. NET framwork SDK environment. Generally, you can install the vs.net tool. Here we use vs2005: as shown in the figure, we enter the command line form of vs2005.


 

First, we use a text file to create. in fact, this file extension of CS can be set by yourself, and does not need to be used. CS is also acceptable. Create a file named simpleprogram. CS enter the following code


Using system;
Public class simpleprogram
{
Static void main ()
{
Console. writeline ("hello ");
Console. Readline ();
}
}

Save it. Pay attention to the Case sensitivity. Then input: CSC simpleprogram. CS in the command line window. Of course, we must first enter the directory where the file is located. If the file is compiled, A simpleprogram.exe will be displayed in the same folder, and a console program will print hello.

Next we will talk about compiling multiple source files.

There are several ways to compile multiple source files: 1. compile all the source files into an EXE file. 2. compile some files into DLL files, compile some files into EXE files, and use them together as an application.

The first method is to create two. CS files.

Program2.cs

Using System
Public class program2
{
Static void main ()
{
Person P = new person ("dotlive ");
Console. writeline ("our person's name is" + P. Name );
}
}


Person. CS

Using system;
Public class person
{
Private string name;
Public Person (string S)
{
Name = s;
}
Public string name
{
Get {
Return name;
}
Set
{
Name = value;
}
}
}


 

Save and enter the file directory in the command line window. Enter CSC program2.cs person. CS will generate an EXE file in the directory. The file name is the first one. CS file names are the same, if. there is also a simple way to write many CS files: CSC *. CS can put all the files in the current directory. compile the CS file.

Now let's talk about the second method, or use the above two. CS files.

First, enter the CSC/T: library person. CS command to generate a person. dll file. Then, enter the command CSC program2.cs/R: person. dll in the second step to generate a program2.exe file that uses the person.dll.

Here two parameters/t and/R appear

/T specifies whether to compile and generate DLL or EXE. In the first method, run the command CSC *. CS is actually a short name, and the complete writing is CSC/T: EXE *. CS, since/T: EXE is the default option, you can leave it empty. Also, the EXE files here are all console applications (other ones will be mentioned later)

/R can be viewed as referencing a DLL file in the format of CSC program. CS/R: 1.dll; 2.dll; 3.dll

Mount multiple DLL, separated by semicolons.

Next, let's talk about the parameter/out, which is used to name the executable file, for example, CSC/out: myapp.exe program2.cs person. cs. Then we can generate an executable file called myapp.exe.

 

What should I do if a main function exists in multiple. CS files?

First, we add a main function in the person class of the person. CS file.

Using system;
Public class person
{
Private string name;
Public Person (string S)
{
Name = s;
}
Public string name
{
Get {
Return name;
}
Set
{
Name = value;
}
}
// Add a new main function.
Static void main ()
{
Person P = new person ("dotlive2 ");
Console. writeline ("our person's name is" + P. Name );
}

}

 

In this way, if two files are compiled into the same EXE at the same time according to the original method, errors will occur.

At this time, we will introduce a new parameter/main

You can enter the command when deciding which main function in the CS file to use.

CSC program2.cs person. CS/main program2

The main function in program2 is used as the program entry point.

Related Article

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.