Section 2.2. Developing


2.2. 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.

This book tacitly assumes that you'll be using Visual Studio .NET. However, 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 with the extension .cs, and compile them with the C# command-line compiler that is distributed with the .NET Framework SDK (or a .NET-compatible development toolchain such as Mono or Microsoft's Shared Source CLI). 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.

2.2.1. 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.

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


To open your application, select Visual C# in the Project Types window, and choose Console Application in the Templates window (if you use the Express Edition of Visual C#, you don't need to perform that first step; go directly to the Console Application).

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.

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 directive for System, System.Collections.Generic, and System.Text because nearly every program you write will need types from those namespaces.

Visual Studio .NET creates a class named Program, which you are free to rename. When you rename the class, it's a good idea to rename the file as well (Class1.cs). If you rename the file, Visual Studio will automatically rename the class for you. To reproduce Example 2-1, for instance, rename the Program.cs file (listed in the Solution Explorer window) to hello.cs and change the name of Program to HelloWorld (if you do this in the reverse order, Visual Studio will rename the class to hello).

Finally, Visual Studio 2005 creates a program skeleton to get you started. To reproduce Example 2-1, remove the arguments (string[] args) 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 aren't 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.

2.2.2. Compiling and Running "Hello World"

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

Keyboard shortcuts can be set by going to Tools Options Keyboard. This book assumes you have chosen the default settings.


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 toolbar (you may need to right-click the toolbar to show the Build toolbar). The Build toolbar is shown in Figure 2-3; the Build button is leftmost and highlighted.

Figure 2-3. Build toolbar


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 may need to customize your toolbar to make this button available). 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.

Figure 2-4. Start Without Debugging button


I strongly recommend that you spend some time exploring the Visual Studio 2005 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 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.


Use the following steps to compile and run the "Hello World" program using the C# command-line compiler that comes with the .NET Framework SDK, Mono (http://www.mono-project.com), or Shared Source CLI (http://msdn.microsoft.com/net/sscli/):

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

  2. Open a .NET command window (Start Programs Visual Studio .NET Visual Studio Tools Visual Studio Command Prompt. If you're on Unix, you should start at a text console, xterm, or something that gives you a shell prompt.

  3. From the command line, use this command if you are using the .NET or Shared Source CLI C# compiler:

    csc /debug hello.cs

  4. Use this command if you are using Mono:

    mcs -debug hello.cs

  5. This step builds the EXE file. If the program contains errors, the compiler reports them in the command window. The /debug command-line switch inserts symbols in the code so that you can run the EXE under a debugger or see line numbers in stack traces. (You'll get a stack trace if your program generates an error that you don't handle.)

  6. To run the program under .NET, enter:

    hello

  7. Use this command with the Shared Source CLI:

    clix hello.exe

  8. and this command with Mono:

    mono hello.exe

You should now see the venerable words "Hello World" appear in your command window.

Just in Time Compilation

Compiling hello.cs using csc creates an EXE file. Keep in mind, however, that the .exe file contains op-codes written in MSIL, which is introduced in Chapter 1.

Interestingly, if you write this application in VB.NET or any other language compliant with the .NET CLS, you will have compiled it into more or less the same MSIL. By design, IL code created from different languages is virtually indistinguishable.

In addition to producing the IL code (which is similar in spirit to Java's bytecode), the compiler creates a read-only segment of the .exe file in which it inserts a standard Win32 executable header. The compiler designates an entry point within the read-only segment; the operating system loader jumps to that entry point when you run the program, just as it would for any Windows program.

The operating system can't execute the IL code, however, and that entry point does nothing but jump to the .NET JIT compiler (also introduced in Chapter 1). The JIT produces native CPU instructions, as you might find in a normal .exe. The key feature of a JIT compiler, however, is that functions are compiled only as they are used, just in time for execution.




Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net