Console Applications


You use console applications regularly in this book, particularly to start off with, so the following Try It Out provides a step-by-step guide to the creation of a simple one.

Try It Out – Creating a Simple Console Application

image from book
  1. Create a new console application project by selecting the File New Project... menu item, as shown in Figure 2-5.

    image from book
    Figure 2-5

  2. Select the Visual C# node in the Project Types: pane of the window that appears, and the Console Application project type in the Templates: pane. Change the Location: text box to C:\BegVCSharp\Chapter2 (this directory will be created automatically if it doesn't already exist), and leave the default text in the Name: text box (ConsoleApplication1) and the other settings as they are. This is shown in Figure 2-6.

    image from book
    Figure 2-6

  3. Click the OK button.

  4. Once the project is initialized, add the following lines of code to the file displayed in the main window:

    namespace ConsoleApplication1 {    class Program    {       static void Main(string[] args)       { // Output text to the screen. Console.WriteLine("The first app in Beginning C# Programming!"); Console.ReadKey();       }    } }

  5. Select the Debug Start menu item. After a few moments you should see the window shown in Figure 2-7.

    image from book
    Figure 2-7

  6. Press a key to exit the application (you may need to click on the console window to focus on it first).

    Note

    Note that the preceding display only appears if the Visual C# Developer settings are applied, as described earlier in this chapter. For example, with Visual Basic Developer settings applied, an empty console window is displayed, and application output appears in a window labeled QuickConsole. In this case, the Console.ReadKey() code also fails, and you will see an error. If you experience this problem, the best solution for working through the examples in this book is to apply the Visual C# Developer settings — that way the results you see will match the results shown here. Should this problem persist then open the Tools Options dialog and uncheck the Debugging Redirect all output to the Quick Console option as shown in Figure 2-8.

    image from book
    Figure 2-8

How It Works

For now, I won't dissect the code you have used in this project, because the focus here is on how to use VS to get code up and running. As you can see, VS does an awful lot for you and makes the process of compiling and executing code very simple. In fact, there are multiple ways of performing even these simple steps. For example, creating a new project can be achieved using the File@ New Project... menu item as mentioned earlier, or by pressing Ctrl+Shift+N, or by clicking on the corresponding icon in the toolbar.

Similarly, your code can be compiled and executed in several ways. The process you used above — selecting the Debug Start menu item — also has a keyboard shortcut (F5) and a toolbar icon. You can also run code without debugging mode using the Debug Start without debugging menu item (also by pressing Ctrl+F5), or compile your project without running it (with debugging on or off) using Build Build Solution or F6. Note that executing a project without debugging or building a project can be done using toolbar icons, although these icons don't appear on the toolbar by default. Once you have compiled your code, you can also execute it simply by running the .exe file produced in Windows Explorer, or from the command prompt. To do this, you open a command prompt window, change the directory to C:\BegVCSharp\Chapter2\ConsoleApplication1\bin\Debug\, type ConsoleApplication1, and press return.

Note

In future examples, I'll just say "create a new console project" or "execute the code," and you can choose whichever method you wish to perform these steps. Unless otherwise stated, all code should be run with debugging enabled. Also, note that the terms "start," "execute," and "run" are used interchangeably in this book, and that discussions following examples always assume that you have exited the application in the example.

One point to note here is that console applications will terminate as soon as they finish execution, which can mean that you don't get a chance to see the results. To get around this in the preceding example, the code is told to wait for a key press before terminating, using the line:

Console.ReadKey(); 

You will see this technique used many times in later examples.

Now that you've created a project, you can take a more detailed look at some of the regions of the development environment.

image from book

The Solution Explorer

The first window to look at is the Solution Explorer window in the top right of the screen. This window shares space with another useful window called Class View, which you can display using the View Class View menu item. Figure 2-9 shows both of these windows with all nodes expanded (you can toggle between them by clicking on the tabs at the bottom of the window).

image from book
Figure 2-9

This Solution Explorer view shows the files that make up the ConsoleApplication1 project. The file you added code to, Program.cs, is shown along with another code file, AssemblyInfo.cs, and a resource file, Resources.resx.

Note

All C# code files have a .cs file extension.

These other files aren't ones that you have to worry about for the moment. They contain extra information about your project that doesn't concern you yet.

You can use this window to change what code is displayed in the main window by double-clicking on .cs files, right-clicking on them and selecting View Code, or selecting them and clicking on the toolbar button that appears at the top of the window. You can also perform other operations on files here, such as renaming them or deleting them from your project. Other types of files can appear here as well, such as project resources (resources are files used by the project that might not be C# files, such as bitmap images and sound files). Again, you can manipulate them through the same interface.

The References entry contains a list of the .NET libraries you are using in your project. Again, this is something you will look at later; the standard references are fine for you to get started with.

The other view, Class View, presents an alternative view of your project by looking at the structure of the code you have created. You will come back to this later in the book; for now the Solution Explorer display is the display of choice.

As you click on files or other icons in these windows, you may notice that the contents of the Properties window (shown in Figure 2-10) changes.

image from book
Figure 2-10

The Properties Window

This window (which you can display using the View Properties Window menu option if you haven't already done so) shows additional information about whatever you select in the window above it. For example, the view shown in Figure 2-10 is displayed when the Program.cs file from the project is selected. This window will also display information about other things that might be selected, such as user interface components (as you will see in the "Windows Forms Applications" section of this chapter).

Often, changes you make to entries in the Properties window will affect your code directly, adding lines of code or changing what you have in your files. With some projects, you spend as much time manipulating things through this window as making manual code changes.

Next, you will look at the Error List window.

The Error List Window

Currently, the Error List window (View Error List) isn't showing much of interest at all. This is because there is nothing wrong with the application. However, this is a very useful window indeed. As a test, try removing the semicolon from one of the lines of code you added in the last section. After a moment, you should see a display like the one shown in Figure 2-11.

image from book
Figure 2-11

Also, the project will no longer compile.

Note

In Chapter 3, when you start looking at C# syntax, you will see how semicolons are expected throughout your code — at the end of most lines in fact.

This window will help you eradicate bugs in your code, because it keeps track of what you have to do in order to compile projects. If you double-click the error shown here, the cursor will jump to the position of the error in your source code (the source file containing the error will be opened if it isn't already open), so you can fix it quickly. You will also see red wavy lines at the positions of errors in the code, so you can quickly scan the source code to see where problems lie.

Note that the error location was specified as a line number. By default, line numbers aren't displayed in the VS text editor, but this is something that is well worth turning on. To do this, you need to tick the relevant check box in the Options dialog, obtained through the Tools Options... menu item. The check box is called Line numbers, and is found in the Text Editor C# General category, as shown in Figure 2-12.

image from book
Figure 2-12

There are many useful options that can be found through this dialog, and you will use several of them later in this book.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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