Professional C# 6 and .NET Core 1.0. Christian Nagel
Чтение книги онлайн.
Читать онлайн книгу Professional C# 6 and .NET Core 1.0 - Christian Nagel страница 12
After writing the source code, you need to compile the code to run it.
You can compile this program by simply running the C# command-line compiler (csc.exe) against the source file, like this:
If you want to compile code from the command line using the csc command, you should be aware that the .NET command-line tools, including csc, are available only if certain environment variables have been set up. Depending on how you installed .NET (and Visual Studio), this may or may not be the case on your machine.
NOTE If you do not have the environment variables set up, you have three options: The first is to add the path to the call of the csc executable. It is located at %Program Files%\MsBuild\14.0\Bin\csc.exe With the dotnet tools installed, you can also find the csc at %ProgramFiles%\dot.net\bin\csc.exe. The second option is to run the batch file %Microsoft Visual Studio 2015 %\Common7\Tools\vsvars32.bat from the command prompt before running csc, where %Microsoft Visual Studio 2015 % is the folder to which Visual Studio 2015 has been installed. The third, and easiest, way is to use the Visual Studio 2015 command prompt instead of the Windows command prompt. To find the Visual Studio 2015 command prompt from the Start menu, select Programs ➪ Microsoft Visual Studio 2015 ➪ Visual Studio Tools. The Visual Studio 2015 command prompt is simply a command prompt window that automatically runs vsvars32.bat when it opens.
Compiling the code produces an executable file named HelloWorld.exe, which you can run from the command line. You can also run it from Windows Explorer as you would run any other executable. Give it a try:
Compiling an executable this way produces an assembly that contains Intermediate Language (IL) code. The assembly can be read using the Intermediate Language Disassembler (IL DASM) tool. If you run ildasm.exe and open HelloWorld.exe, you see that the assembly contains a Program type and a Main method as shown in Figure 1.3.
Double-click the MANIFEST node in the tree view to reveal metadata information about the assembly (see Figure 1.4). This assembly makes use of the mscorlib assembly (because the Console class is located there), and some configuration and version of the HelloWorld assembly.
Double-click the Main method to reveal the IL code of this method (see Figure 1.5). No matter what version of the Hello, World code you compiled, the result is the same. The string Hello, World! is loaded before calling the method System.Console.WriteLine that is defined within the mscorlib assembly passing the string. One feature of the CLR is the JIT compiler. The JIT compiler compiles IL code to native code when running the application.
Using the new .NET Core Command line (CLI), some preparations need to be done to compile the application without the help of Visual Studio. Let’s have a look at the new tools next to compile the Hello, World sample application.
Setting Up the Environment
In case you have Visual Studio 2015 with the latest updates installed, you can immediately start with the CLI tools. Otherwise, you need to install .NET Core and the CLI tools. You can find instructions for the download at http://dotnet.github.io for Windows, Linux, and OS X.
With Windows, different versions of .NET Core runtimes as well as NuGet packages are installed in the user profile. As you work with .NET, this folder increases in size. Over time as you create multiple projects, NuGet packages are no longer stored in the project itself; they’re stored in this user-specific folder. This has the advantage that you do not need to download NuGet packages for every different project. After you have this NuGet package downloaded, it’s on your system. Just as different versions of the NuGet packages as well as the runtime are available, all the different versions are stored in this folder. From time to time it might be interesting to check this folder and delete old versions you no longer need.
Installing .NET Core CLI tools, you have the dotnet tools as an entry point to start all these tools. Just start
to see all the different options of the dotnet tools available.
The repl (read, eval, print, loop') command is good to learn and test simple features of C# without the need to create a program. Start repl with the dotnet tool:
This starts an interactive repl session. You can enter the following statements for a Hello, World using a variable:
The output you’ll see as you enter the last statement is the Hello, World! string.
The dotnet repl command is not available with Preview 2 of the tools, but it will be available at a later time as an extension.
Building the Application
The dotnet tools offer an easy way to create a Hello, World application. You create a new directory HelloWorldApp, and change to this directory with the command prompt. Then enter this command:
This command creates a Program.cs file that includes the code for the Hello, World program, a NuGet.config file that defines the NuGet server where NuGet packages should be loaded, and project.json, the new project configuration file.
NOTE With dotnet new you can also create the initial files needed for libraries and ASP.NET web applications (the option – template will be available with RTM). You can also select other programming languages, such as F# and Visual Basic (with the option – lang).
The created project configuration file is named project.json. This file is in JavaScript Object Notation (JSON) format and defines the framework application information such as version, description, authors, tags, dependencies to libraries, and the frameworks that are supported by the application. The generated project configuration file is shown in the following code snippet (code file HelloWorldApp/project.json):
With the buildOptions settings, the emitEntryPoint is set. This is necessary if you create a Main method as a program entry point. This Main method is invoked when you run the application. This setting is not needed with libraries.
With the dependencies section, you can add all dependencies of the program, such