A. Downloading and Installing the C# Compiler and CLI Platform

To compile and run C# programs, it is necessary to install a version of the compiler and the CLI platform.

Microsoft .NET for Windows

The predominant CLI platform is Microsoft .NET, which is the platform of choice for development on Microsoft Windows.

For a rich IDE that includes IntelliSense and support for project files, install a version of the Visual Studio IDE. The “Community Edition” of Visual Studio 2015 is free and contains everything you need to make server applications, rich client applications, and cross-platform mobile applications. This edition, along with other editions of Visual Studio, is available at http://visualstudio.com.

Microsoft provides free, redistributable packages that include the .NET Framework and compiler binaries for all versions of .NET going back to version 2.0. These are available in the downloads section at http://www.microsoft.com/net. At the time of this book’s writing, the latest released version is .NET 4.6.

Visual Studio Compilation

In 2015, Microsoft made Visual Studio 2015 Community1 available for free. This is a full version of the company’s IDE that provides a premium platform for .NET software development. One of the primary advantages of developing with Visual Studio is the ability to create and open entire projects of files and even combine such projects together into solutions.

1. Visual Studio Express was available or free prior to 2015 and is also sufficient for compiling all samples in this book save those depending on C# 6.0 features.

The associated source code2 includes a solution file, EssentialCSharp.sln, that can be opened up with Visual Studio 2015, compiled (built), and run. After opening the solution file, use the Build->Build Solution menu to compile the code.

2. The source code available for this book (along with some chapters related to earlier versions of C#) is available for download at IntelliTect.com/EssentialCSharp. You can also download the code from http://itl.tc/EssentialCSharpSCC.

Before you can execute the source code, you need to select which project to execute by selecting the associated chapter’s project as the startup project. For example, to execute the samples in Chapter 1, you would right-click on the Chapter01 project and choose Set As Startup Project. Failure to choose the correct chapter will result in an exception with the message (“Error, could not run the Listing....”) when you specify the listing number at execution time.

Once you have selected the correct project, you can run the project from the Debug->Start Without Debugging menu. Alternatively, if you wish to debug the project, you can use Debug->Start Debugging. Once running, the program will prompt for the listing (for example, 18.33) that you wish to execute. As mentioned earlier, you can enter only listings from the project that was set to start up.

Many of the listings have corresponding unit tests. To execute a particular test, open the test project and navigate to the test corresponding to the listing you wish to execute. From there, right-click on the test method and choose either Run Tests (Ctrl+R, T) or Debug Tests (Ctrl+R, Ctrl+T).

Setting up the Compiler Path for Command-Line Compilation

For command-line compilation, regardless of whether you are working with Visual Studio or just the runtime, you must set the PATH environment variable to include the C# compiler, CSC.EXE. If Visual Studio is installed on your computer, open the command prompt from the Start menu by selecting the Developer Command Prompt shortcut from the Visual Studio section of the Windows Start menu. This command prompt places CSC.EXE in the path to be available for execution from any directory.

Without Visual Studio installed, no special compiler command prompt item appears in the Start menu. Instead, you need to reference the full compiler pathname explicitly or add it to the path. The compiler is located at %Windir%Microsoft.NETFramework<version>, where <version> is the version of the .NET Framework (typically v4.0.30319, which contains the compiler tools for all versions of .NET since .NET 4.0) and %Windir% is the environment variable that points to the location of the Windows directory. To add this location to the path, use Set PATH=%PATH%;%Windir%Microsoft.NETFramework<version>, again substituting the value of <version> appropriately. Output A.1 provides an example.

OUTPUT A.1

Set PATH=%PATH%;%Windir%Microsoft.NETFrameworkv4.0.30319

Once the path includes the framework, you can then use the .NET C# compiler, CSC.EXE, without providing the full path to its location.

.NET on OS X and Linux

Since its initial release, there has always been cross platforms versions of the CLI that allow execution of .NET and CIL code on operating systems in addition to Windows. As of this writing, the key platforms are the .NET Core project (CoreCLR) and Mono. Both of these are open source implementations that allow development on OS X and Linux (in addition to Windows).

.NET Core

Microsoft provides an open-source version of the “.NET Core“ version of the .NET Framework for OS X and Linux (in addition to Windows); at the time of this book’s writing, it was in preview release with development driven primarily by ASP.NET 5. Sources and binary downloads are available at https://github.com/dotnet/coreclr.

Unlike the full .NET Framework on Windows, the .NET Core project does not require installing the .NET Framework as part of the operating system. Rather, all the files you need are copied to a single directory. To prepare the environment requires downloading some general-purpose packages so that .NET can be installed. Next, it is necessary to install the .NET Version Manager followed by the .NET Core Execution Environment (DNX). This environment provides the engine for executing CIL under the CoreCLR implementation. Finally, once everything is set up, the environment is ready to compile and run your C# code.

Execution of managed executables created with the .NET Framework is built into the Windows operating system, but not into OS X or Linux (or even on Windows with .NET Core). To execute the generated binary, you use a utility that launches the executable in the CLR; it is activated by executing dnx.exe.

For complete and up-to-date instructions for installation and execution using .NET Core, see http://itl.tc/GettingStartedWithDNX.

Note that the Core .NET project on OS X and Linux is intended for writing high-performance server-side code, not for writing graphical user interface applications; there is no GUI library package included with Core .NET. To make applications with user interfaces on non-Windows platforms, use Mono.

Mono

For CLI development on platforms other than Microsoft Windows, consider Mono, which is a platform you can download at http://www.mono-project.com. As with the .NET platform, Mono requires the full path to the C# compiler if it is not already in the search path. The default installation path on Linux is /usr/lib/mono/<version>, and the compiler is gmcs.exe or mcs.exe, depending on the version. (If Mono is installed on Microsoft Windows, the default path is %ProgramFiles%Mono-<version>libmono<version>.)

One option for a Linux version that includes an installation of Mono is Monoppix. This version builds on the CD-bootable Linux distribution known as Knoppix and is available for download at http://www.monoppix.com.

Instead of CSC.EXE, the Mono platform’s compiler is MCS.EXE or GMCS.EXE, depending on the compiler version. Therefore, the command for compiling HelloWorld.cs is as shown in Output A.2.

OUTPUT A.2

C:SAMPLES>msc.exe HelloWorld.cs

Unfortunately, the Linux environment cannot run the resultant binaries directly. Instead, it requires explicit execution of the runtime using mono.exe, as shown in Output A.3.

OUTPUT A.3

C:SAMPLES>mono.exe HelloWorld.exe
Hello. My name is Inigo Montoya.

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

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