Introduction
In this exercise, you will use Microsoft Visual Studio. NET to create, compile, and run a Hello World application.
Create a new project in IDE
1. Choose start> Programs> Microsoft Visual Studio. NET7.0> Microsoft Visual Studio. NET 7.0. IDE. The start page is displayed, as shown in figure 1.
Figure 1 start page in Visual Studio. NET |
Note: When you start IDE, the start page is opened by default. It provides connections to the four projects you recently created and other resource links.
You can choose tools> options to change the default settings and select the settings you like.
2. click File> New> Project to display the New Project window. See figure 2.
The default project location is Documents and SettingsAdministratorMy DocumentsVisual Studio Projects.
Figure 2 new project window |
3. Under the Project type panel, click Visual C # Projects.
4. On the Templates panel, click Empty Project.
5. Enter hello In the name field and select the storage location for your project.
6. Click OK.
Create a hello. cs source file
The hello. cs file is the source file used in this exercise. You will create this file in the hello project.
1. Click File and then Add New Item. The Add New Item window is displayed. See figure 3.
2. on the Categories panel, click Local Project Items.
3. On the Templates panel, click Code File.
4. Input hello. cs in the name field and click Open.
Figure 3 Add a new entry window |
5. Enter the following code:
Using System; Public class HelloWorld { Static void Main (){ Console. WriteLine ("Hello World! "); } } |
6. Click File and then Save hello. cs (or click the Sava icon). The File is stored in the project folder.
Figure 4 Hello. cs source file |
Note:
In C #, there is no global method. All methods belong to one class.
The. NET Framework requires that all method names start with an uppercase letter, so a Main () method can work in the. NET Framework, and a main () method won't work.
The using System line tells the compiler that all classes and types from System namespace can be accessed and used in this program. System namespace includes some basic types, such as int and string.
Public class HelloWorld declares a class, which means this class can be accessed by other classes.
Static void Main () is the entry of the program. This entry must appear at the beginning of each executable file of a program.
Console. WriteLine ("Hello World! "); This line uses the method WriteLine () in the Console class in the system namespace. The parameter" Hello World! "Passed to Console. WriteLine (). When the program is executed, it will be displayed on the screen.
Console. WriteLine () can replace System. Console. WriteLine () because the System namespace has been declared at the top of the file.
Generate executable files
Click the Build menu option on the Build menu (or click the Build icon) to generate an executable file.
Note: IDE generates an executable file and places it in the C: Documents and SettingsAdministratorMy DocumentsVisual Studio ProjectshelloinDebug folder.
The output window in IDE is displayed during generation. See Figure 5. If any errors occur during the generation process, these errors will be displayed in the output window.
Figure 5 output window in IDE |
Run the program
You can run programs in IDE
1. Select Debug> Start Without Debugging, Hello World! Will appear in Microsoft MS-DOS? Window. (figure 6)
Note: Since this is a very small executable file that is used to output text in the MS-DOS window, you can turn off the debugging feature to run it. In this way, you can directly see the results.
Remember: In "Hello World! "Press any key to continue" is not part of the program.
When the execution file is run on the. NET platform for the first time, it will be compiled into local code. This will delay the result display, and the execution file will be faster at the next run.
C # The Compiler does not generate the target file (. obj), but directly generates the output file. In this way, the C # compiler does not need the linker.
Figure 6 program running result |
2. Close the MS-DOS window.
End
Click File and then Exit to close hello world! Applications