Developing “Hello World”

There are at least two ways to enter, compile, and run the programs in this book: use the Visual Studio .NET Integrated Development Environment (IDE), or use a text editor and a command-line compiler (along with some additional command-line tools to be introduced later).

Although you can develop software outside Visual Studio .NET, the IDE provides enormous advantages. These include indentation support, Intellisense word completion, color coding, and integration with the help files. Most important, the IDE includes a powerful debugger and a wealth of other tools.

Although this book tacitly assumes that you’ll be using Visual Studio .NET, the tutorials focus more on the language and the platform than on the tools. You can copy all the examples into a text editor such as Windows Notepad or Emacs, save them as text files, and compile them with the C# command-line compiler that is distributed with the .NET Framework SDK. Note that some examples in later chapters use Visual Studio .NET tools for creating Windows Forms and Web Forms, but even these you can write by hand in Notepad if you are determined to do things the hard way.

Editing “Hello World”

To create the “Hello World” program in the IDE, select Visual Studio .NET from your Start menu or a desktop icon, and then choose File New Project from the menu toolbar. This will invoke the New Project window (if you are using Visual Studio for the first time, the New Project window might appear without further prompting). Figure 2-1 shows the New Project window.

Creating a C# console application in Visual Studio .NET

Figure 2-1. Creating a C# console application in Visual Studio .NET

To open your application, select Visual C# Projects in the Project Type window and select Console Application in the Template window. You can now enter a name for the project and select a directory in which to store your files. Click OK, and a new window will appear in which you can enter the code in Example 2-1, as shown in Figure 2-2.

The editor opened to your new project

Figure 2-2. The editor opened to your new project

Notice that Visual Studio .NET creates a namespace based on the project name you’ve provided (HelloWorld), and adds a using System statement because nearly every program you write will need types from the System namespace.

Visual Studio .NET creates a class named Class1, which you are free to rename. When you rename the class, be sure to rename the file as well (Class1.cs). To reproduce Example 2-1, for instance, you change the name of Class1 to HelloWorld, and rename the Class1.cs file (listed in the Solution Explorer window) to HelloWorld.cs.

Finally, Visual Studio .NET creates a program skeleton, complete with a TODO comment to get you started. To reproduce Example 2-1, remove the arguments (string[] args) and comments from the Main( ) method. Then copy the following two lines into the body of Main( ):

// Use the system console object 
System.Console.WriteLine("Hello World");

If you are not using Visual Studio .NET, open Notepad, type in the code from Example 2-1, and save the file as a text file named Hello.cs.

Compiling and Running “Hello World”

There are many ways to compile and run the “Hello World” program from within Visual Studio .NET. Typically you can accomplish every task by choosing commands from the Visual Studio .NET menu toolbar, by using buttons, and, in many cases, by using key-combination shortcuts.

For example, to compile the “Hello World” program, press Ctrl-Shift-B or choose Build Build Solution. As an alternative, you can click the Build button on the Build button bar. The Build button icon is shown in Figure 2-3.

Build button icon

Figure 2-3. Build button icon

To run the “Hello World” program without the debugger you can press Ctrl-F5 on your keyboard, choose Debug Start Without Debugging from the IDE menu toolbar, or press the Start Without Debugging button on the IDE Build toolbar, as shown in Figure 2-4. You can run the program without first explicitly building it; depending on how your options are set (Tools Options) the IDE will save the file, build it, and run it, possibly asking you for permission at each step.

Start without debugging button

Figure 2-4. Start without debugging button

Tip

I strongly recommend that you spend some time exploring the Visual Studio .NET development environment. This is your principal tool as a .NET developer, and you want to learn to use it well. Time invested up front in getting comfortable with Visual Studio .NET will pay for itself many times over in the coming months. Go ahead, put the book down and look at it. I’ll wait for you.

To compile and run the “Hello World” program using the C# command-line compiler:

  1. Save Example 2-1 as the file hello.cs.

  2. Open a command window (Start->Run and type in cmd).

  3. From the command line, enter:

    csc hello.cs

    This step will build the executable (EXE) file. If the program contains errors, the compiler will report them in the command window.

  4. To run the program, enter:

    Hello

    You should see the venerable words “Hello World” appear in your command window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.197