2
Writing a C# Program

THE VISUAL STUDIO 2017 DEVELOPMENT ENVIRONMENT

When you begin the installation of Visual Studio Community 2017 you are prompted with a window similar to Figure 2‐1 . It provides a list of Workloads, Individual components, and Language packs to install along with the core editor.

Install the following Workloads and click the Install button.

After installation is complete, when Visual Studio is first loaded, it immediately presents you with the option to Sign in to Visual Studio using your Microsoft Account. By doing this, your Visual Studio settings are synced between devices so that you do not have to configure the IDE when using it on multiple workstations. If you do not have a Microsoft Account, follow the process for the creation of one and then use it to sign in. If you do not want to sign in, click the “Not now, maybe later” link, and continue the initial configuration of Visual Studio. We recommend that at some point you sign in and get a developer license.

Screenshot illustration of an installation of Visual Studio Community 2017 with a window.

FIGURE 2‐1

If this is the first time you've run Visual Studio, you will be presented with a list of preferences intended for users who have experience with previous releases of this development environment. The choices you make here affect a number of things, such as the layout of windows, the way that console windows run, and so on. Therefore, choose Visual C# from the drop‐down; otherwise, you might find that things don't quite work as described in this book. Note that the options available vary depending on the options you chose when installing Visual Studio, but as long as you chose to install C# this option will be available.

If this isn't the first time that you've run Visual Studio, and you chose a different option the first time, don't panic. To reset the settings to Visual C#, you simply have to import them. To do this, select Tools image Import and Export Settings, and choose the Reset All Settings option, shown in Figure 2‐2 .

Click Next, and indicate whether you want to save your existing settings before proceeding. If you have customized things, you might want to do this; otherwise, select No and click Next again. From the next dialog box, select Visual C#, shown in Figure 2‐3 . Again, the available options may vary.

Screenshot illustration of how to run Visual Studio by selecting Tools➪ Import and Export Settings, and choose the Reset All Settings option.

FIGURE 2‐2

Screenshot illustration of how to save the existing settings before proceeding to select Visual C#.

FIGURE 2‐3

Finally, click Finish, then Close to apply the settings.

The Visual Studio environment layout is completely customizable, but the default is fine here. With C# Developer Settings selected, it is arranged as shown in Figure 2‐4 .

Screenshot illustration of the arrangement of the Visual Studio environment layout.

FIGURE 2‐4

The main window, which contains a helpful Start Page by default when Visual Studio is started, is where all your code is displayed. This window can contain many documents, each indicated by a tab, so you can easily switch between several files by clicking their filenames. It also has other functions: It can display GUIs that you are designing for your projects, plain‐text files, HTML, and various tools that are built into Visual Studio. You will come across all of these in the course of this book.

Above the main window are toolbars and the Visual Studio menu. Several different toolbars can be placed here, with functionality ranging from saving and loading files to building and running projects to debugging controls. Again, you are introduced to these as you need to use them.

Here are brief descriptions of each of the main features that you will use the most:

This might seem like a lot to take in, but it doesn't take long to get comfortable. You start by building the first of your example projects, which involves many of the Visual Studio elements just described.

CONSOLE APPLICATIONS

You use console applications regularly in this book, particularly at the beginning, so the following Try It Out provides a step‐by‐step guide to creating a simple one.

The Solution Explorer

By default, the Solution Explorer window is docked in the top‐right corner of the screen. As with other windows, you can move it wherever you like, or you can set it to auto‐hide by clicking the pin icon. The Solution Explorer window shares space with another useful window called Class View, which you can display using View image Class View. Figure 2‐8 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 when the window is docked).

Screenshot illustration of both the Solution Explorer window and another useful window called Class View, with all nodes expanded.

FIGURE 2‐8

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

You don't have to worry about the AssemblyInfo.cs file for the moment. It contains 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 .cs files; right‐clicking them and selecting View Code; or by selecting them and clicking 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 file types can also appear here, 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.

You can also expand code items such as Program.cs to see what is contained. This overview of your code structure can be a very useful tool; it also enables you to navigate directly to specific parts of your code file, instead of opening the code file and scrolling to the part you want.

The References entry contains a list of the .NET libraries you are using in your project. You'll look at this later; the standard references are fine for now. Class View presents an alternative view of your project by showing the structure of the code you created. You'll come back to this later in the book; for now the Solution Explorer display is appropriate. As you click on files or other icons in these windows, notice that the contents of the Properties window (shown in Figure 2‐9 ) changes.

Screenshot illustration showing the contents of the Properties window, as one clicks on files or other icons in the 2 windows.

FIGURE 2‐9

The Properties Window

The Properties window (select View image Properties Window if it isn't already displayed) shows additional information about whatever you select in the window above it. For example, the view shown in Figure 2‐9 is displayed when the Program.cs file from the project is selected. This window also displays information about other selected items, such as user interface components (as shown in the “Desktop Applications” section of this chapter).

Often, changes you make to entries in the Properties window 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.

The Error List Window

Currently, the Error List window (View image Error List) isn't showing anything interesting because there is nothing wrong with the application. However, this is a very useful window indeed. As a test, remove the semicolon from one of the lines of code you added in the previous section. After a moment, you should see a display like the one shown in Figure 2‐10 .

Screenshot illustration of a display of the Error List window (View➪Error List).

FIGURE 2‐10

In addition, the project will no longer compile.

This window helps you eradicate bugs in your code because it keeps track of what you have to do to compile projects. If you double‐click the error shown here, the cursor jumps 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. Red wavy lines appear at the positions of errors in the code, so you can quickly scan the source code to see where problems lie.

The error location is specified as a line number. By default, line numbers aren't displayed in the Visual Studio text editor, but that is something well worth turning on. To do so, tick the Line numbers check box in the Options dialog box (selected via the Tools image Options menu item). It appears in the Text Editor image All Languages image General category.

You can also change this setting on a per‐language basis through the language‐specific settings pages in the dialog box. Many other useful options can be found through this dialog box, and you will use several of them later in this book.

DESKTOP APPLICATIONS

It is often easier to demonstrate code by running it as part of a desktop application than through a console window or via a command prompt. You can do this using user interface building blocks to piece together a user interface.

The following Try It Out shows just the basics of doing this, and you'll see how to get a desktop application up and running without a lot of details about what the application is actually doing. You'll use WPF here, which is Microsoft's recommended technology for creating desktop applications. Later, you take a detailed look at desktop applications and learn much more about what WPF is and what it's capable of.

image WHAT YOU LEARNED IN THIS CHAPTER

TOPIC KEY CONCEPTS
Visual Studio 2017 settings This book requires the C# development settings option, which you choose when you first run Visual Studio or by resetting the settings.
Console applications Console applications are simple command‐line applications, used in much of this book to illustrate techniques. Create a new console application with the Console Application template that you see when you create a new project in Visual Studio. To run a project in debug mode, use the Debug image Start Debugging menu item, or press F5.
IDE windows The project contents are shown in the Solution Explorer window. The properties of the selected item are shown in the Properties window. Errors are shown in the Error List window.
Desktop applications Desktop applications are applications that have the look and feel of standard Windows applications, including the familiar icons to maximize, minimize, and close an application. They are created with the WPF Application template in the New Project dialog box.