The tools

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:

  • How do you build a program, or otherwise prepare it for deployment?
  • How do you debug a program? Quickly figuring out what the problem is, and where it is when there is one. This is just as important as writing the program in the first place.

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.

Visual Studio

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).

Full versions of Visual Studio

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.

  • Professional: This is the base commercial package. It allows you to build all available projects, in all available languages. In the context of C#, some of the project types available are ASP.NET WebForms, ASP.NET MVC, Windows 8 App, Windows Phone, Silverlight, Library, Console, along with a robust testing framework.
  • Premium: In this version, all professional features are included, in addition to the code metrics, expanded testing tools, architecture diagramming, lab management, and project management features.
  • Ultimate: This version includes code clone analysis, more testing tools (including Microsoft Fakes), and IntelliTrace, in addition to all the features of the previous levels.

Check out these versions at http://www.microsoft.com/visualstudio/11/enus/products/visualstudio.

Licensing

There are several different options for licensing the full version of Visual Studio.

  • MSDN Subscription: The Microsoft Developer Network provides a subscription service where you can pay an annual fee to gain access to versions of Visual Studio. Additionally, you can get an MSDN Subscription as part of Microsoft's MVP program, which rewards the active community members in the development community. You can find more information about purchasing an MSDN Subscription at https://msdn.microsoft.com/en-us/subscriptions/buy/buy.aspx.
  • BizSpark: If you are creating a startup, Microsoft offers the BizSpark program to give you access to Microsoft software (including Visual Studio) at no cost for three years. After your graduation date, you keep the licenses that you've downloaded over the course of the program, and get discounts on MSDN Subscriptions, in addition to other alumni benefits. BizSpark is a great option for any entrepreneur that wants to use the Microsoft technology stack. Find out if you qualify for the BizSpark program at http://www.microsoft.com/bizspark.
  • DreamSpark: Students can enroll in the DreamSpark program, which lets you download Visual Studio Professional (in addition to other applications and servers). As long as you are a student in a valid academic institution, you will have access to everything you need to develop applications using C#.Students. Sign up today at https://www.dreamspark.com/.
  • Individual and Volume licensing: If none of the previous options for the commercial version of Visual Studio are appropriate, then you can always purchase licenses directly from Microsoft or various resellers at http://www.microsoft.com/visualstudio/en-us/buy/small-midsize-business.

Express

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:

  • Visual Studio Express 2012 for Windows 8: This is for creating Metro style applications for Windows 8
  • Visual Studio Express 2012 for Windows Phone: This lets you write programs for Microsoft's Windows Phone devices
  • Visual Studio Express 2012 for Web: All web applications can be built using this version of Visual Studio, from ASP.NET (forms and MVC), to Azure hosted projects
  • Visual Studio Express 2012 for Desktop: Applications that target the classic Windows 8 Desktop environment can be built with this version.

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:

  • Express versions of Visual Studio are limited by vertical stack, meaning you have to install a separate product for each project type that is supported (Web, desktop, phone, and so on). This is hardly a huge limitation though, and would only be a burden in the most complex of solutions.
  • There are no plugins. There are many productivity enhancing plugins that are available for the full version of Visual Studio, so for some users this exclusion can be a big deal. However, the good news is that one of the most popular plugins in recent memory, NuGet, is now being shipped with all versions of Visual Studio 2012. NuGet helps you manage your project's library dependencies. You can browse through the NuGet catalog and add open source third-party libraries, in addition to libraries from Microsoft.

The express versions of Visual Studio can be downloaded from http://www.microsoft.com/visualstudio/11/en-us/products/express.

Using Visual Studio

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:

  1. Launch Visual Studio, or if you are using Express, launch Visual Studio Express for Desktop.
  2. Create a new project by clicking on File | New Project….
  3. Choose Console Application from Installed | Templates | Visual C#.
  4. Give the project a name such as program, and click on OK.
  5. Add a line of code in the Main method as follows:
    Console.WriteLine("Hello World");
  6. Run the program by choosing Debug | Run without Debugger.

You will see the expected Hello World output and you are now ready to start using Visual Studio.

Command-line compiler

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:

Note

Note that the .NET 4.5 version of the .NET Framework will actually replace the 4.0 framework if you have that installed. That's why the path mentioned previously shows as v4.0.30319. You won't be the first person confused by versions in the .NET Framework.

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.

SharpDevelop

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:

  1. Start SharpDevelop.
  2. Create a new project by clicking on File | New | Solution.
  3. Choose Console Application from C# | Windows Application.
  4. Give the project a name such as program, and click on Create.
  5. Right-click on the project node in the Projects window, and choose the Properties menu item; check the Compiling tab to see if the Target Framework says .NET Framework 4.0 Client Profile.
  6. If it does, then simply click on the Change button, select .NET Framework 4.5 in the Change Target Framework drop-down menu, and finally click on the Convert button.
  7. Run the program by choosing Debug | Run without Debugger.

You will see the expected Hello World output.

MonoDevelop

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:

  1. Start MonoDevelop.
  2. Create a new project by clicking on File | New | Solution….
  3. Choose Console Application from the C# menu.
  4. Give the project a name such as program, and click on Forward, then on OK.
  5. Right-click on the project node in the Solution window, and choose the Options menu item. Now go to Build | General to see if the Target Framework says Mono / .NET 4.0.
  6. If it does, then simply choose .NET Framework 4.5 from the dropdown and click on the OK button.
  7. Run the program by choosing Run | Start without Debugging.

If all goes well, you will see a terminal window (if running on OS X, for example) with the Hello World text.

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

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