Whenever you approach a new programming language, or a tool, there are several questions that you can ask yourself in order to quickly become proficient in that environment, such as:
In the following sections, we will review several tools that are available to you in order to get a development environment up and running on your local machine. These options vary across a number of different licensing terms and cost structures. No matter your situation or preferences, you will able to get a development environment up and running and you will be able to answer the previous questions by the end of the chapter.
Microsoft provides the de facto compiler and development environment for the C# language. Although the compiler is available as a command-line executable since the first release of the .NET Framework, most developers will stay within the confines of Visual Studio, which is Microsoft's Integrated Development Environment (IDE).
Microsoft's full commercial offerings of Visual Studio come in several different versions, each with a cumulative number of features as you move up the ladder.
Check out these versions at http://www.microsoft.com/visualstudio/11/enus/products/visualstudio.
There are several different options for licensing the full version of Visual Studio.
The Visual Studio Express product line is a nearly fully featured version of Visual Studio that is free of cost. Anyone can download these products and begin learning and developing at no charge.
The available versions are as follows:
It's a common misconception that Visual Studio Express may only be used for non-commercial projects, but this is not the case. You are entirely free to develop and release a commercial product while still adhering to the EULA. The only limitations are technical, as follows:
The express versions of Visual Studio can be downloaded from http://www.microsoft.com/visualstudio/11/en-us/products/express.
Regardless of which version of Visual Studio you decide to use, getting started is very simple once the product has been installed. The following are the steps:
program
, and click on OK.Main
method as follows:Console.WriteLine("Hello World");
You will see the expected Hello World output and you are now ready to start using Visual Studio.
If you prefer to work at a lower level than with an IDE like Visual Studio, you can always opt to simply use the command-line compiler directly. Microsoft provides everything you need to compile C# code entirely for free by downloading and installing the .NET 4.5 Redistributable package from http://www.microsoft.com/en-us/download/details.aspx?id=8483.
Once that's downloaded and installed, you can find the compiler at C:windowsmicrosoft.netFrameworkv4.0.30319csc.exe
, assuming you maintain all of the default installation options:
A small tip that will make working with the command-line compiler much easier is to simply add it to the environment's Path
variable. If you're using PowerShell (which I highly encourage), you can easily do so by running the following command:
PS ~> $env:Path += ";C:WindowsMicrosoft.NETFrameworkv4.0.30319"
That makes it so you can just type csc
instead of the whole path. Usage of the command-line compiler is very simple, take the following class:
using System; namespace program { class MainClass { static void Main (string[] args) { Console.WriteLine("Hello, World"); } } }
Save this class as a file named program.cs
using your favorite text editor. Once saved, you can compile it from the command line using the following command:
PS ~ookcodech1> csc .ch1_hello.cs
This will produce an executable file named ch1_hello.exe
, which when executed, will produce a familiar greeting as follows:
PS ~ookcodech1> .ch1_hello.exe Hello, World
By default, csc
will output an executable file. However, you can also produce libraries using the target argument. Consider the following class:
using System; namespace program { public class Greeter { public void Greet(string name) { Console.WriteLine("Hello, " + name); } } }
This class encapsulates the functionality of the previous program, and even makes it reusable by letting you define the name to be greeted. Although this is a somewhat trite example, the point is to show how to create a .dll
file that you can use from multiple programs.
PS ~devookcodech1> csc /target:library .ch1_greeter.cs
An assembly named ch1_greeter.dll
will be generated, which you can then use from a slightly modified version of the previous program as follows:
using System; namespace program { class MainClass { static void Main (string[] args) { Greeter greeter = new Greeter(); greeter.Greet("Componentized World"); } } }
If you try to compile the previous program just as you did before, the compiler will rightly complain about not knowing anything about the Greeter
class as follows:
PS ~ookcodech1> csc .ch1_greeter_program.cs Microsoft (R) Visual C# Compiler version 4.0.30319.17626 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved. ch1_greeter_program.cs(9,13): error CS0246: The type or namespace name 'Greeter' could not be found (are you missing a using directive or an assembly reference?) ch1_greeter_program.cs(9,35): error CS0246: The type or namespace name 'Greeter' could not be found (are you missing a using directive or an assembly reference?)
Any time you have an error in your programs, it will be shown in the output, along with information about the file it was found in, and the line, so you can find it easily. In order for this to work, you will have to tell the compiler to use the ch1_greeter.dll
file that you created using the /r:
argument as follows:
PS ~ookcodech1> csc /r:ch1_greeter.dll .ch1_greeter_program.cs
And now when you run the resulting ch1_greeter_program.exe
program, you will see the output say, Hello, Componentized World.
Though most developers will not use the command-line compiler directly these days, it is good to know that it is available and also how to use it, especially if you have to support advanced scenarios such as merging multiple modules into a single assembly.
When you launch SharpDevelop, the tagline on the loading screen, The Open Source .NET IDE, is a concise description. since the very early days of the .NET Framework, it provided developers a free option for writing C# before Microsoft shipped the Express versions. Since that time, it has continued to mature, and add features, and as of version 4.2, SharpDevelop supports targeting the .NET 4.5, and more specifically, compilation and debugging of C# 5.0. Although Visual Studio Express is a compelling option, the lack of source control plugins can be a deal breaker for some users. Thankfully, SharpDevelop will gladly let you integrate with a source control server in the IDE. Additionally, some of the more niche project types such as creating Windows Services (one of the few project types not supported by Express) are fully supported with SharpDevelop.
Projects use the same format (.sln
, .csproj
) as Visual Studio, so project portability is high. You can usually take a project written in Visual Studio and open it in SharpDevelop.
Download the application from http://www.icsharpcode.net/OpenSource/SD/.
Installation is straightforward, and you can verify correct installation by creating the following sample program:
program
, and click on Create.You will see the expected Hello World output.
The Mono framework is an open source version of the Common Language Runtime and C#. It has had over a decade of active development, and as a result, is very mature and stable. There are versions of Mono for just about any platform you might be interested in developing for Windows, OS X, Unix/Linux, Android, iOS, PlayStation Vita, Wii, and Xbox 360.
MonoDevelop is based on SharpDevelop, but was forked some time ago to specifically act as a development environment for Mono that would run on multiple platforms. It runs on Windows, OS X, Ubuntu, Debian, SLE, and openSUSE; so, as a developer, you can truly choose what platform you want to work on.
You can get started by installing the Mono Development Kit 2.11 or higher from http://www.go-mono.com/mono-downloads/download.html.
Once you have installed that for your platform, you can go ahead and install the latest version of MonoDevelop from http://monodevelop.com/.
Using the C# 5.0 compiler is but a few short steps away:
program
, and click on Forward, then on OK.If all goes well, you will see a terminal window (if running on OS X, for example) with the Hello World text.