Writing Your First Program

6 313 0
Writing Your First Program

Đang tải... (xem toàn văn)

Thông tin tài liệu

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# 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 1. 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# 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. 2. Type a period immediately after Console. Another Intellisense list appears displaying the methods, properties, and fields of the Console class. 3. 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 } 4. 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. 5. Type a close parenthesis, followed by a semicolon. Main should now look like this: static void Main(string[] args) { Console.WriteLine(); } 6. 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# 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 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: 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 1. On the Build menu, click Build Solution. This action causes the C# 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, 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. 2. 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: 3. 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. 4. 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# 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. 5. In the Solution Explorer, click the + to the left of the bin entry. Another folder named Debug appears. 6. 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# compiler. You must first complete the following steps to set up your environment: 1. 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. 2. 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: 3. cd \Documents and Settings\YourName\My Documents\Microsoft Press\Visual CSharp Step by Step\Chapter 1\TextHello\TextHello 4. Type the following command: csc /out:TextHello.exe Program.cs This command creates the executable file TextHello.exe from the C# 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. 5. 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. . Writing Your First Program The Program. cs file defines a class called Program that contains a method called Main 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

Ngày đăng: 28/10/2013, 20:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan