Datasheet
circumstances in some of the languages that compile to managed code. Indeed, pointers (as opposed to
references) are permitted only in marked blocks of code in C#, and not at all in Visual Basic (although
they are allowed in managed C++). Using pointers in your code causes it to fail the memory type safety
checks performed by the CLR.
You should note that some languages compatible with .NET, such as Visual Basic 2005, still allow some
laxity in typing, but that is only possible because the compilers behind the scenes ensure that the type
safety is enforced in the emitted IL.
Although enforcing type safety might initially appear to hurt performance, in many cases the benefits
gained from the services provided by .NET that rely on type safety far outweigh this performance loss.
Such services include:
❑ Language interoperability
❑ Garbage collection
❑ Security
❑ Application domains
The following sections take a closer look at why strong data typing is particularly important for these
features of .NET.
The Importance of Strong Data Typing for Language Interoperability
If a class is to derive from or contains instances of other classes, it needs to know about all the data types
used by the other classes. This is why strong data typing is so important. Indeed, it is the absence of any
agreed-on system for specifying this information in the past that has always been the real barrier to inheri-
tance and interoperability across languages. This kind of information is simply not present in a standard
executable file or DLL.
Suppose that one of the methods of a Visual Basic 2005 class is defined to return an
Integer — one of
the standard data types available in Visual Basic 2005. C# simply does not have any data type of that name.
Clearly, you will only be able to derive from the class, use this method, and use the return type from C#
code, if the compiler knows how to map Visual Basic 2005’s
Integer type to some known type that is
defined in C#. So, how is this problem circumvented in .NET?
Common Type System
This data type problem is solved in .NET through the use of the Common Type System (CTS). The CTS
defines the predefined data types that are available in IL, so that all languages that target the .NET
Framework will produce compiled code that is ultimately based on these types.
For the previous example, Visual Basic 2005’s
Integer is actually a 32-bit signed integer, which maps
exactly to the IL type known as
Int32. This will therefore be the data type specified in the IL code.
Because the C# compiler is aware of this type, there is no problem. At source code level, C# refers to
Int32 with the keyword int, so the compiler will simply treat the Visual Basic 2005 method as if it
returned an
int.
10
Part I: The C# Language
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 10