Название: Professional C# 6 and .NET Core 1.0
Автор: Christian Nagel
Издательство: John Wiley & Sons Limited
Жанр: Зарубежная образовательная литература
isbn: 9781119096634
isbn:
• HelloWorldApp
• VariablesSample
• VariableScopeSample
• IfStatement
• ForLoop
• EnumerationsSample
• NamespacesSample
• ArgumentsSample
• StringSample
Now that you understand more about what C# can do, you need to know how to use it. This chapter gives you a good start in that direction by providing a basic understanding of the fundamentals of C# programming, which is built on in subsequent chapters. By the end of this chapter, you will know enough C# to write simple programs (though without using inheritance or other object-oriented features, which are covered in later chapters).
Chapter 1, “.NET Application Architectures,” explains how to write a “Hello, World!” C# program using the csc Compiler for .NET 4.6 and using dotnet tools for .NET Core 1.0. You can also create it with Visual Studio 2015, which is done in this chapter.
NOTE In the first chapters of this book, Visual Studio is used as a code editor and compiler without employing all the other features of Visual Studio. Chapter 17, “Visual Studio 2015,” covers more about all the other options and features offered by Visual Studio.
Creating a Solution
First, create a solution file within Visual 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.
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.
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.
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).
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.
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).
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 СКАЧАТЬ