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.

Working with Visual studio 2010 ❘ 395<br />

file was generally known as a make file <strong>and</strong> is still quite st<strong>and</strong>ard on UNIX systems. Make files are not<br />

normally needed on Windows, although you can still write them (or get Visual Studio to generate them) if<br />

you need to.<br />

Debugging <strong>and</strong> release Builds<br />

The idea of having separate builds is very familiar to C++ developers, <strong>and</strong> to a lesser degree, to those<br />

with a Visual Basic background. The point here is that when you are debugging, you typically want your<br />

executable to behave differently from when you are ready to ship the software. When you are ready to ship<br />

your software, you want the size of the executable to be as small as possible <strong>and</strong> the executable itself to<br />

be as fast as possible. Unfortunately, these requirements are not compatible with your needs when you are<br />

debugging code, as explained in the following sections.<br />

Optimization<br />

High performance is achieved partly by the compiler doing many optimizations on the code. This means<br />

that the compiler actively looks at your source code as it is compiling to identify places where it can<br />

modify the precise details of what you are doing in a way that does not change the overall effect, but that makes<br />

things more efficient. For example, if the compiler encountered the following source code:<br />

double InchesToCm(double Ins)<br />

{<br />

return Ins*2.54;<br />

}<br />

// later on in the code<br />

Y = InchesToCm(X);<br />

it might replace it with this:<br />

Y = X * 2.54;<br />

Or it might replace this code:<br />

{<br />

}<br />

with this:<br />

string Message = "Hi";<br />

Console.WriteLine(Message);<br />

Console.WriteLine("Hi");<br />

By doing so, it bypasses having to declare an unnecessary object reference in the process.<br />

It is not possible to exactly pin down what optimizations the <strong>C#</strong> compiler does — nor whether the two<br />

previous examples actually would occur with any particular example — because those kinds of details<br />

are not documented. (Chances are that for managed languages such as <strong>C#</strong>, the previous optimizations<br />

would occur at JIT compilation time, not when the <strong>C#</strong> compiler compiles source code to assembly.) For<br />

obvious commercial reasons, companies that write compilers are usually quite reluctant to give too many<br />

details about the tricks that their compilers use. We should stress that optimizations do not affect your<br />

source code — they affect only the contents of the executable code. However, the previous examples should<br />

give you a good idea of what to expect from optimizations.<br />

The problem is that although optimizations like the previous ones help a great deal in making your code<br />

run faster, they are not that helpful for debugging. Suppose that, in the first example, you want to set a<br />

breakpoint inside the InchesToCm() method to see what is going on in there. How can you possibly do that<br />

if the executable code does not actually have an InchesToCm() method because the compiler has removed<br />

it Moreover, how can you set a watch on the Message variable when that does not exist in the compiled<br />

code either<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!