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.”)
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.
-
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.
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.
-
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, 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).