3.3 Creating a Simple App in Visual Studio

Now that we’ve presented our first console app (Fig. 3.1), we provide a step-by-step explanation of how to create, compile and execute it using Visual Studio Community 2015, which we’ll refer to simply as Visual Studio from this point forward.

3.3.1 Creating the Console App

After opening Visual Studio, select File > New > Project… to display the New Project dialog (Fig. 3.3). At the left side of the dialog, under Installed > Templates > Visual C# select the Windows category, then in the middle of the dialog select the Console Application template. In the dialog’s Name field, type Welcome1—your project’s name—then click OK to create the project. By default, the project’s folder will be placed in your user account’s documents folder under visual studio 2015Projects.

The IDE now contains the open console app (Fig. 3.4). The editor window already contains some code provided by the IDE. Some of this code is similar to that of Fig. 3.1. Some is not and uses features that we have not yet discussed. The IDE inserts this extra code to help organize the app and to provide access to some common classes in the .NET Framework Class Library—at this point in the book, this code is neither required nor relevant to the discussion of this app; delete all of it.

Fig. 3.3 Selecting Console Application in the New Project dialog.

Fig. 3.4 IDE with an open console app’s code displayed in the editor and the project’s contents shown in the Solution Explorer at the IDE’s top-right side.

The code coloring scheme used by the IDE is called syntax-color highlighting and helps you visually differentiate code elements. For example, keywords appear in blue and comments appear in green. We color our code similarly as discussed in the Preface. One example of a literal is the string passed to Console.WriteLine in line 10 of Fig. 3.1. You can customize the colors shown in the code editor by selecting Tools > Options…. This displays the Options dialog. Then expand the Environment node and select Fonts and Colors. Here you can change the colors for various code elements. Visual Studio provides many ways to personalize your coding experience.

3.3.2 Changing the Name of the App File

For the apps we create in this book, we change the source-code file’s default name (Program.cs) to a more descriptive name. To rename the file, right click Program.cs in the Solution Explorer and select Rename to make the file name editable. Windows automatically selects the file name’s base part (i.e., Program). Type Welcome1, then press Enter to change the name to Welcome1.cs. Be sure to keep the .cs filename extension.

Error-Prevention Tip 3.4

When changing a file name in a Visual Studio project, always do so in Visual Studio. Changing file names outside the IDE can break the project and prevent it from executing.

3.3.3 Writing Code and Using IntelliSense

In the editor window (Fig. 3.4), replace the generated code with Fig. 3.1’s code. As you begin typing the name Console (line 10), an IntelliSense window is displayed (Fig. 3.5).

Fig. 3.5 IntelliSense window as you type Con.

As you type, IntelliSense lists various items that start with or contain the letters you’ve typed so far. IntelliSense also displays a tool tip containing a description of the first matching item. You can either type the complete item name (e.g., Console), double click the item name in the member list or press the Tab key to complete the name. Once the complete name is provided, the IntelliSense window closes. While the IntelliSense window is displayed, pressing the Ctrl key makes the window transparent so you can see the code behind the window.

When you type the dot (.) after Console, the IntelliSense window reappears and initially shows only class Console’s members that can be used on the right of the dot—as you type, this list narrows to items containing what you’ve typed so far. Figure 3.6 shows the IntelliSense window narrowed down to only items that contain “Write”. You also can type “WL” to find all items containing the capital letters “W” and “L” (such as WriteLine).

Fig. 3.6 IntelliSense window.

When you type the opening parenthesis character, (, after Console.WriteLine, the Parameter Info window is displayed (Fig. 3.7). This contains information about the method’s parameters—the data methods require to perform their tasks. As you’ll learn in Chapter 7, a method can have several versions. That is, a class can define several methods that have the same name, as long as they have different numbers and/or types of parameters—a concept known as overloaded methods. These methods normally all perform similar tasks. The Parameter Info window indicates how many versions of the selected method are available and provides up and down arrows for scrolling through the different versions. For example, there are many versions of the WriteLine method that enable you to display different types of data—we use the one that displays a string in our app. The Parameter Info window is one of many features provided by the IDE to facilitate app development. In the next several chapters, you’ll learn more about the information displayed in these windows. The Parameter Info window is especially helpful when you want to see the different ways in which a method can be used. From the code in Fig. 3.1, we already know that we intend to display one string with WriteLine, so, because you know exactly which version of WriteLine you want to use, you can close the Parameter Info window by pressing the Esc key, or simply keep typing and ignore it. After you type the app’s code, select File > Save All to save the project.

Fig. 3.7 Parameter Info window.

3.3.4 Compiling and Running the App

You’re now ready to compile and execute your app. Depending on the project’s type, the compiler may compile the code into files with the .exe (executable) extension, the .dll (dynamically linked library) extension or one of several other extensions—you can find these files in project’s subfolders on disk. Such files are called assemblies and are the packaging units for compiled C# code. These assemblies contain the Microsoft Intermediate Language (MSIL; Section 1.9.2) code for the app.

To compile the app, select Build > Build Solution. If the app contains no compile-time errors, this will compile your app and build it into an executable file (named Welcome1.exe, in one of the project’s subdirectories). To execute it, type Ctrl + F5, which invokes the Main method (Fig. 3.1). If you attempt to run the app before building it, the IDE will build the app first, then run it only if there are no compilation errors. The statement in line 10 of Main displays Welcome to C# Programming!. Figure 3.8 shows the results of executing this app, displayed in a console (Command Prompt) window. Leave the app’s project open in Visual Studio; we’ll go back to it later in this section. [Note: The console window normally has a black background and white text. We reconfigured it to have a white background and black text for readability. If you’d like to do this, right click anywhere in the Command Prompt window’s title bar, then select Properties. You can change the colors in the Colors tab of the dialog that appears.]

Fig. 3.8 Executing the app shown in Fig. 3.1.

3.3.5 Errors, Error Messages and the Error List Window

Go back to the app in Visual Studio. As you type code, the IDE responds either by applying syntax-color highlighting or by generating an error. When an error occurs, the IDE underlines the error’s location with a red squiggly line and provides a description of it in the Error List window (Fig. 3.9). If the Error List window is not visible, select View > Error List to display it. In Fig. 3.9, we intentionally omitted the semicolon at the end of line 10. The error message indicates that the semicolon is missing. You can double click an error message in the Error List to jump to the error’s location in the code.

Error-Prevention Tip 3.5

One compile-time error can lead to multiple entries in the Error List window. Each error you correct could eliminate several subsequent error messages when you recompile your app. So when you see an error you know how to fix, correct it—the IDE will recompile your code in the background, so fixing an error may make several other errors disappear.

 

Fig. 3.9 Syntax error indicated by the IDE.

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

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