15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24 ❘ ChaPTer 2 cOre c#<br />

Later chapters present a number of code samples. The most common technique for<br />

writing <strong>C#</strong> programs is to use Visual Studio 2010 to generate a basic project <strong>and</strong> add<br />

your own code to it. However, because the aim of Part I is to teach the <strong>C#</strong> language,<br />

we are going to keep things simple <strong>and</strong> avoid relying on Visual Studio 2010 until<br />

Chapter 16, “ Visual Studio 2010. ” Instead, we will present the code as simple fi les that<br />

you can type in using any text editor <strong>and</strong> compile from the comm<strong>and</strong> line.<br />

The Code<br />

Type the following into a text editor (such as Notepad), <strong>and</strong> save it with a .cs extension (for example,<br />

First.cs ). The Main() method is shown here (for more information, see “ The Main Method ” section later<br />

in this chapter):<br />

using System;<br />

namespace Wrox<br />

{<br />

public class MyFirstClass<br />

{<br />

static void Main()<br />

{<br />

Console.WriteLine("Hello from Wrox.");<br />

Console.ReadLine();<br />

return;<br />

}<br />

}<br />

}<br />

code snippet First.cs<br />

Compiling <strong>and</strong> running the Program<br />

You can compile this program by simply running the <strong>C#</strong> comm<strong>and</strong> - line compiler ( csc.exe ) against the<br />

source fi le, like this:<br />

csc First.cs<br />

If you want to compile code from the comm<strong>and</strong> line using the csc comm<strong>and</strong>, you should be aware that the<br />

.<strong>NET</strong> comm<strong>and</strong> - line tools, including csc , are available only if certain environment variables have been set<br />

up. Depending on how you installed .<strong>NET</strong> (<strong>and</strong> Visual Studio 2010), this may or may not be the case on<br />

your machine.<br />

If you do not have the environment variables set up, you have the following two<br />

options: The fi rst is to run the batch fi le %Microsoft Visual Studio 2010%\<br />

Common7\Tools\vsvars32.bat from the comm<strong>and</strong> prompt before running csc , where<br />

%Microsoft Visual Studio 2010% is the folder to which Visual Studio 2010 has<br />

been installed. The second, <strong>and</strong> easier, way is to use the Visual Studio 2010 comm<strong>and</strong><br />

prompt instead of the usual comm<strong>and</strong> prompt window. You will fi nd the Visual Studio<br />

2010 comm<strong>and</strong> prompt in the Start menu, under Programs, Microsoft Visual Studio<br />

2010, Visual Studio Tools. It is simply a comm<strong>and</strong> prompt window that automatically<br />

runs vsvars32.bat when it opens.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!