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.

8 ❘ ChaPTer 1 .net Architecture<br />

This is all quite an ambitious aim, but amazingly, .<strong>NET</strong> <strong>and</strong> IL have achieved it. In the case of stepping<br />

between methods in the debugger, this facility is really offered by the Visual Studio integrated development<br />

environment (IDE) rather than by the CLR itself.<br />

distinct Value <strong>and</strong> reference Types<br />

As with any programming language, IL provides a number of predefined primitive data types. One<br />

characteristic of IL, however, is that it makes a strong distinction between value <strong>and</strong> reference types. Value<br />

types are those for which a variable directly stores its data, whereas reference types are those for which a<br />

variable simply stores the address at which the corresponding data can be found.<br />

In C++ terms, using reference types is similar to accessing a variable through a pointer, whereas for Visual<br />

Basic, the best analogy for reference types are objects, which in Visual Basic 6 are always accessed through<br />

references. IL also lays down specifications about data storage: instances of reference types are always<br />

stored in an area of memory known as the managed heap, whereas value types are normally stored on the<br />

stack (although if value types are declared as fields within reference types, they will be stored inline on<br />

the heap). Chapter 2, “Core <strong>C#</strong>,” discusses the stack <strong>and</strong> the heap <strong>and</strong> how they work.<br />

strong data Typing<br />

One very important aspect of IL is that it is based on exceptionally strong data typing. That means that all<br />

variables are clearly marked as being of a particular, specific data type (there is no room in IL, for example,<br />

for the Variant data type recognized by Visual Basic <strong>and</strong> scripting languages). In particular, IL does not<br />

normally permit any operations that result in ambiguous data types.<br />

For instance, Visual Basic 6 developers are used to being able to pass variables around without worrying too<br />

much about their types, because Visual Basic 6 automatically performs type conversion. C++ developers are<br />

used to routinely casting pointers between different types. Being able to perform this kind of operation can<br />

be great for performance, but it breaks type safety. Hence, it is permitted only under certain circumstances<br />

in some of the languages that compile to managed code. Indeed, pointers (as opposed to references) are<br />

permitted only in marked blocks of code in <strong>C#</strong>, <strong>and</strong> not at all in Visual Basic (although they are allowed in<br />

managed C++). Using pointers in your code causes it to fail the memory type-safety checks performed by<br />

the CLR. You should note that some languages compatible with .<strong>NET</strong>, such as Visual Basic 2010, still allow<br />

some laxity in typing, but that it’s possible only because the compilers behind the scenes ensure that the type<br />

safety is enforced in the emitted IL.<br />

Although enforcing type safety might initially appear to hurt performance, in many cases the benefits<br />

gained from the services provided by .<strong>NET</strong> that rely on type safety far outweigh this performance loss. Such<br />

services include the following:<br />

➤<br />

➤<br />

➤<br />

➤<br />

Language interoperability<br />

Garbage collection<br />

Security<br />

Application domains<br />

The following sections take a closer look at why strong data typing is particularly important for these<br />

features of .<strong>NET</strong>.<br />

strong Data Typing as a Key to language interoperability<br />

If a class is to derive from or contains instances of other classes, it needs to know about all the data<br />

types used by the other classes. This is why strong data typing is so important. Indeed, it is the absence<br />

of any agreed-on system for specifying this information in the past that has always been the real barrier<br />

to inheritance <strong>and</strong> interoperability across languages. This kind of information is simply not present in a<br />

st<strong>and</strong>ard executable file or DLL.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!