Professional C# 6 and .NET Core 1.0. Christian Nagel

Читать онлайн книгу.

Professional C# 6 and .NET Core 1.0 - Christian Nagel


Скачать книгу
Studio. A solution enables you to group multiple projects and to open all the projects of a solution together.

You can create an empty solution by selecting File ➪ New Project and then selecting Installed ➪ Templates ➪ Other Project Types ➪ Visual Studio Solutions. Select the Blank Solution template (see Figure 2.1). With the New Project dialog, you can define the name of the solution as well as the directory where the solution should be stored. You can also define whether the solution should be added to a Git repository for source control management.

Figure 2.1

After creating the solution, you see the content of the solution within the Solution Explorer (see Figure 2.2). Currently, there’s only a solution file without any content.

Figure 2.2

      Creating a New Project

Now add a new project to create the Hello, World! app. Right-click the solution in Solution Explorer, or use the Menu button on the keyboard to open the context menu (refer to Figure 2.2), and open the application context menu and select Add ➪ New Project to open the Add New Project dialog. Alternatively, you can select File ➪ Add ➪ New Project. In the Add New Project dialog, select the Console Application (Package) template to create a console application targeting .NET Core. You can find this project type in the tree within Installed ➪ Templates ➪ Visual C# ➪ Web (see Figure 2.3). Set the name of the application to HelloWorldApp.

Figure 2.3

      NOTE To open the context menu of an application, you have different options: right-click while selecting the item where the context menu should be opened (or left-click if you are left-handed), or select the item and press the menu key on the keyboard (usually located between the Alt and Ctrl keys on the right side). If your keyboard doesn’t have a menu key, press Shift + F10. Lastly, if you have a touch pad, you can make a two-finger touch.

The Solution Explorer is no longer empty. It now shows the project and all the files belonging to the project (see Figure 2.4).

Figure 2.4

      In Chapter 1, the project file was created by the dotnet tool, now it is created from a Visual Studio template. Two Frameworks – .NET 4.6 and .NET Core 1.0 are specified. With both frameworks, the NetStandard.Library 1.0 is referenced (code file HelloWorldApp/project.json):

      The generated C# source file Program.cs contains a Main method within the Program class that itself is defined within the namespace HelloWorldApp (code file HelloWorldApp/Program.cs):

      Change this to the Hello, World! app. You need to open the namespace for using the WriteLine method of the Console class, and you need to invoke the WriteLine method. You also change the namespace for the Program class. The Program class is now defined within the namespace Wrox.HelloWorldApp (code file HelloWorldApp/Program.cs):

Select the project in Solution Explorer and use the context menu to open Properties (or View ➪ Property Pages) to open the project configuration (see Figure 2.5). On the Application tab, you can select the name of the application, the default namespace (this is only used for new items added), and the version of the .NET Core version that should be used for the solution. In case you select a version that is different from your default selection, a global.json file is created that contains this configuration setting.

Figure 2.5

      Compiling and Running the Program

      The Build menu offers different options for building the program. You can either use Build ➪ Build Solution to build all projects of the solution, or you can build a single project with Build ➪ Build HelloWorldApp. Also have a look at the other options available with the Build menu.

To generate persistent files, you can check the Produce Outputs on Build option on the Build tab in the project properties (see Figure 2.6).

Screenshot shows a project configuration window for HelloWorldApp with highlighted build tab, marked checkbox for produce outputs on build and unmarked checkbox for Compile TypeScript on build.

Figure 2.6

      After building the program with the Produce Outputs on Build option selected, you can see in File Explorer the directory artifacts that contains subdirectories for all the supported .NET Framework versions listed with the binaries.

You can run the application from within Visual Studio by using Debug ➪ Start Without Debugging. This starts the app as shown in Figure 2.7.

Screenshot shows a command prompt window with message Hello World! Press any key to continue.

Figure 2.7

      NOTE Be sure to not start the app with Debug ➪ Start Debugging; if you do you will not see the output of the app because the console window immediately closes after the app completes. You can use this method to run the app either with setting breakpoints and debugging into the app, or by adding a ReadLine method before the end of the Main method.

You can use the Debug tab in the project properties to configure the runtime version that should be used while running the app (see Figure 2.8).

Screenshot shows a project configuration window for HelloWorldApp with highlighted Debug tab on the left side, fields for profile, launch, command et cetera along with Add, Browse, New and Remove buttons.

Figure 2.8

      TIP When you have multiple projects in the same solution, you can define what project should run by selecting the project in Solution Explorer and opening the context menu. In the context menu click Set as Startup Project (or Project ➪ Set as Startup Project). Alternatively, you can select the solution in the Solution Explorer, and select Set Startup Projects to open the property page for the solution where you can select what should be the startup project. You can also define multiple projects to start.

      Taking a Closer Look at the Code

      Now let’s concentrate on the C# source code. First, I have a few general comments about C# syntax. In C#, as in other C-style languages, statements end in a semicolon (;) and can continue over multiple lines without needing a continuation character. Statements


Скачать книгу