Writing Your First Program
2006-07-02 15:38
260 查看
Writing Your First Program
The Program.cs file defines a class called Program that contains a method called Main. All methods must be defined inside a class. The Main method is special—it designates the program's entry point. It must be a static method. (Methods are discussed in Chapter 3, “Writing Methods and Applying Scope.” Static methods are discussed in Chapter 7, “Creating and Managing Classes and Objects.” The Main method is discussed in Chapter 11, “Understanding Parameter Arrays.”)
IMPORTANT
C# 2005 2005 is a case-sensitive language. You must spell Main with a capital M.
In the following exercises, you'll write the code to display the message Hello World in the console; you'll build and run your Hello World console application; you'll learn how namespaces are used to partition code elements.
Write the code using IntelliSense technology
In the Code and Text Editor window displaying the Program.cs file, place the cursor in the Main method after the opening brace, and type Console. As you type the letter C at the start of the word Console an IntelliSense list appears. This list contains all of the valid C# 2005 2005 keywords and data types that are valid in this context. You can either continue typing, or scroll through the list and double-click the Console item with the mouse. Alternatively, after you have typed Con, the Intellisense list will automatically home in on the Console item and you can press the Tab, Enter, or Spacebar key to select it.
Main should look like this:
NOTE
Console is a built-in class that contains the methods for displaying messages on the screen and getting input from the keyboard.
Type a period immediately after Console. Another Intellisense list appears displaying the methods, properties, and fields of the Console class.
Scroll down through the list until WriteLine is selected, and then press Enter. Alter-natively, you can continue typing until WriteLine is selected and then press Enter.
The IntelliSense list closes, and the WriteLine method is added to the source file. Main should now look like this:
Type an open parenthesis. Another IntelliSense tip appears.
This tip displays the parameters of the WriteLine method. In fact, WriteLine is an overloaded method, meaning that Console contains more than one method named Write Line. Each version of the WriteLine method can be used to output different types of data. (Overloaded methods are discussed in Chapter 3.) Main should now look like this:
You can click the tip's up and down arrows to scroll through the overloaded versions of WriteLine.
Type a close parenthesis, followed by a semicolon.
Main should now look like this:
Type the string "Hello World" between the left and right parentheses.
Main should now look like this:
TIP
Get into the habit of typing matched character pairs, such as ( and ) and { and }, before filling in their contents. It's easy to forget the closing character if you wait until after you've entered the contents.
IntelliSense Icons
IntelliSense displays the name of every member of a class. To the left of each member name is an icon that depicts the type of member. The icons and their types include the following:
NOTE
You will frequently see lines of code containing two forward slashes followed by ordinary text. These are comments. They are ignored by the compiler, but are very useful for developers because they help document what a program is actually doing. For example:
All text from the two slashes to the end of the line will be skipped by the compiler. You can also add multi-line comments starting with /*. The compiler will skip everything until it finds a */ sequence, which could be many lines lower down. You are actively encouraged to document your code with as many comments as necessary.
Build and run the console application
On the Build menu, click Build Solution. This action causes the C# 2005 2005 code to be compiled, resulting in a program that you can run. The Output windows appears below the Code and Text Editor window.
TIP
If the Output window does not appear, click the View menu, and then click Output to display it.
In the Output window, messages similar to the following show how the program is being compiled and display the details of any errors that have occurred. In this case there should be no errors or warnings, and the program should build successfully:
NOTE
An asterisk after the file name in the tab above the Code and Text Editor window indicates that the file has been changed since it was last saved. There is no need to manually save the file before building because the Build Solution command automatically saves the file.
On the Debug menu, click Start Without Debugging. A Command window opens and the program runs. The message Hello World appears, and then the program waits for the user to press any key, as shown in the following graphic:
Ensure that the Command window displaying the program has the focus, and then press Enter. The Command window closes and you return to the Visual Studio 2005 programming environment.
NOTE
If you run the program using Start Debugging on the Debug menu, the application runs but the Command window closes immediately without waiting for you to press a key.
In the Solution Explorer, click the TextHello project (not the solution), and then click Show All Files button. Entries named bin and obj appear above the C# 2005 2005 source filenames. These entries correspond directly to folders named bin and obj in the project folder (/My Documents/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello). These folders are created when you build your application, and they contain the executable version of the program and some other files.
In the Solution Explorer, click the + to the left of the bin entry. Another folder named Debug appears.
In the Solution Explorer, click the + to the left of the Debug entry. Three entries named TextHello.exe, TextHello.pdb, and TextHello.vshost.exe appear. The file TextHello.exe is the compiled program, and it is this file that runs when you click Start Without Debugging in the Debug menu. The other two files contain information that is used by Visual Studio 2005 if you run your program in Debug mode (when you click Start Debugging in the Debug menu).
Command Line Compilation
You can also compile your source files into an executable file manually by using the csc command-line C# 2005 2005 compiler. You must first complete the following steps to set up your environment:
On the Windows Start menu, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and click Visual Studio 2005 Command Prompt. A Command window opens, and the envionment variables PATH, LIB, and INCLUDE are configured to include the locations of the various .NET Framework libraries and utilities.
TIP
You can also run the vcvarsall.bat script, located in the C:/Program Files/Microsoft Visual Studio 8/VC folder, if you want to configure the environment variables while running in an ordinary Command Prompt window.
In the Visual Studio 2005 Command Prompt window, type the following command to go to the /My Documents/Microsoft Press/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello project folder:
Type the following command:
This command creates the executable file TextHello.exe from the C# 2005 2005 source file. If you don't use the /out command-line option, the executable file takes its name from the source file and is called Program.exe.
Run the program by typing the following command:
The program should run exactly as before, except that you will not see the "Press any key to continue" prompt.
The Program.cs file defines a class called Program that contains a method called Main. All methods must be defined inside a class. The Main method is special—it designates the program's entry point. It must be a static method. (Methods are discussed in Chapter 3, “Writing Methods and Applying Scope.” Static methods are discussed in Chapter 7, “Creating and Managing Classes and Objects.” The Main method is discussed in Chapter 11, “Understanding Parameter Arrays.”)
IMPORTANT
C# 2005 2005 is a case-sensitive language. You must spell Main with a capital M.
In the following exercises, you'll write the code to display the message Hello World in the console; you'll build and run your Hello World console application; you'll learn how namespaces are used to partition code elements.
Write the code using IntelliSense technology
In the Code and Text Editor window displaying the Program.cs file, place the cursor in the Main method after the opening brace, and type Console. As you type the letter C at the start of the word Console an IntelliSense list appears. This list contains all of the valid C# 2005 2005 keywords and data types that are valid in this context. You can either continue typing, or scroll through the list and double-click the Console item with the mouse. Alternatively, after you have typed Con, the Intellisense list will automatically home in on the Console item and you can press the Tab, Enter, or Spacebar key to select it.
Main should look like this:
static void Main(string[] args) { Console }
NOTE
Console is a built-in class that contains the methods for displaying messages on the screen and getting input from the keyboard.
Type a period immediately after Console. Another Intellisense list appears displaying the methods, properties, and fields of the Console class.
Scroll down through the list until WriteLine is selected, and then press Enter. Alter-natively, you can continue typing until WriteLine is selected and then press Enter.
The IntelliSense list closes, and the WriteLine method is added to the source file. Main should now look like this:
static void Main(string[] args) { Console.WriteLine }
Type an open parenthesis. Another IntelliSense tip appears.
This tip displays the parameters of the WriteLine method. In fact, WriteLine is an overloaded method, meaning that Console contains more than one method named Write Line. Each version of the WriteLine method can be used to output different types of data. (Overloaded methods are discussed in Chapter 3.) Main should now look like this:
static void Main(string[] args) { Console.WriteLine( }
You can click the tip's up and down arrows to scroll through the overloaded versions of WriteLine.
Type a close parenthesis, followed by a semicolon.
Main should now look like this:
static void Main(string[] args) { Console.WriteLine(); }
Type the string "Hello World" between the left and right parentheses.
Main should now look like this:
static void Main(string[] args) { Console.WriteLine("Hello World"); }
TIP
Get into the habit of typing matched character pairs, such as ( and ) and { and }, before filling in their contents. It's easy to forget the closing character if you wait until after you've entered the contents.
IntelliSense Icons
IntelliSense displays the name of every member of a class. To the left of each member name is an icon that depicts the type of member. The icons and their types include the following:
Icon | Meaning |
C# 2005 2005 keyword | |
method (discussed in Chapter 3) | |
property (discussed in Chapter 14) | |
class (discussed in Chapter 7) | |
struct (discussed in Chapter 9) | |
enum (discussed in Chapter 9) | |
interface (discussed in Chapter 12) | |
delegate (discussed in Chapter 16) | |
Namespace |
You will frequently see lines of code containing two forward slashes followed by ordinary text. These are comments. They are ignored by the compiler, but are very useful for developers because they help document what a program is actually doing. For example:
Console.ReadLine(); // Wait for the user to press the Enter key
All text from the two slashes to the end of the line will be skipped by the compiler. You can also add multi-line comments starting with /*. The compiler will skip everything until it finds a */ sequence, which could be many lines lower down. You are actively encouraged to document your code with as many comments as necessary.
Build and run the console application
On the Build menu, click Build Solution. This action causes the C# 2005 2005 code to be compiled, resulting in a program that you can run. The Output windows appears below the Code and Text Editor window.
TIP
If the Output window does not appear, click the View menu, and then click Output to display it.
In the Output window, messages similar to the following show how the program is being compiled and display the details of any errors that have occurred. In this case there should be no errors or warnings, and the program should build successfully:
------ Build started: Project: TextHello, Configuration: Debug Any CPU ---- Csc.exe /config /nowarn:"1701;1702" /errorreport: prompt /warn:4 ...
Compile complete –- 0 errors, 0 warnings
TextHello -> C:/Documents and Settings/John/My Documents/Microsoft Press/... ============ Build: 1 succeeded or up-to-date, aea3 0 failed, 0 skipped ========
NOTE
An asterisk after the file name in the tab above the Code and Text Editor window indicates that the file has been changed since it was last saved. There is no need to manually save the file before building because the Build Solution command automatically saves the file.
On the Debug menu, click Start Without Debugging. A Command window opens and the program runs. The message Hello World appears, and then the program waits for the user to press any key, as shown in the following graphic:
Ensure that the Command window displaying the program has the focus, and then press Enter. The Command window closes and you return to the Visual Studio 2005 programming environment.
NOTE
If you run the program using Start Debugging on the Debug menu, the application runs but the Command window closes immediately without waiting for you to press a key.
In the Solution Explorer, click the TextHello project (not the solution), and then click Show All Files button. Entries named bin and obj appear above the C# 2005 2005 source filenames. These entries correspond directly to folders named bin and obj in the project folder (/My Documents/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello). These folders are created when you build your application, and they contain the executable version of the program and some other files.
In the Solution Explorer, click the + to the left of the bin entry. Another folder named Debug appears.
In the Solution Explorer, click the + to the left of the Debug entry. Three entries named TextHello.exe, TextHello.pdb, and TextHello.vshost.exe appear. The file TextHello.exe is the compiled program, and it is this file that runs when you click Start Without Debugging in the Debug menu. The other two files contain information that is used by Visual Studio 2005 if you run your program in Debug mode (when you click Start Debugging in the Debug menu).
Command Line Compilation
You can also compile your source files into an executable file manually by using the csc command-line C# 2005 2005 compiler. You must first complete the following steps to set up your environment:
On the Windows Start menu, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and click Visual Studio 2005 Command Prompt. A Command window opens, and the envionment variables PATH, LIB, and INCLUDE are configured to include the locations of the various .NET Framework libraries and utilities.
TIP
You can also run the vcvarsall.bat script, located in the C:/Program Files/Microsoft Visual Studio 8/VC folder, if you want to configure the environment variables while running in an ordinary Command Prompt window.
In the Visual Studio 2005 Command Prompt window, type the following command to go to the /My Documents/Microsoft Press/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello project folder:
cd /Documents and Settings/YourName/My Documents/Microsoft Press/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello
Type the following command:
csc /out:TextHello.exe Program.cs
This command creates the executable file TextHello.exe from the C# 2005 2005 source file. If you don't use the /out command-line option, the executable file takes its name from the source file and is called Program.exe.
Run the program by typing the following command:
TextHello
The program should run exactly as before, except that you will not see the "Press any key to continue" prompt.
相关文章推荐
- Installing perl and writing your first perl program in Ubuntu
- # Writing your-first Django-app-part 4-simple-form
- Your First iOS app(10)-- Writing a Custom Class
- 搭建自己的Django应用(1)Writing your first Django app, part 1
- Writing your first Django app, part 1(转)
- # Writing your-first Django-app-part 5 -test
- Making Your Computer Do Something: Writing Your First Procedure
- 搭建自己的Django应用(3)Writing your first Django app, part 3
- Writing Your First Thread Function
- VBA Tips:Your first VBA program
- 在20分钟内写出第一个MapReduce程序Write your first MapReduce program in 20 minutes
- Writing your first academic paper
- Compiling Your First VTK Program
- Writing your first Django app, part 1
- [翻译]Django1.11 Writing your first Django app, part 1
- Why Python to be your first program language?
- Haskell、C#、Java、C 程序执行效率比较 :Write your first parallel Haskell program
- Angel Borja博士教你如何撰写科学论文三:Writing the first draft of your science paper — some dos and don’ts
- Writing your first Django app, part 2
- Writing your first Django