|
|
|
Why Learn Yet Another Language?
It seems that all too often someone comes up
with a new and improved tool to solve the crisis of the day, when
in fact it's the same crisis everyone has been dealing with for
So, why create a new language? After all, there
are so many languages out there to choose from; surely one of them
would suffice to do the job. We have Visual Basic for high-level
abstraction and C++ for
In the same way that some people like the city
life but others prefer the quiet countryside, the choice of
languages is also a personal preference. Of course, language wars
are often debates over language semantics, the expressive power and
performance versus ease of use and high-level constructs such as
object-oriented features. However, one thing has always remained
constant: the need to reach the Holy Grail of software engineering,
code reuse. Or, better yet, component-based development. The
ability to create a component and use it in a software application
that is written in another programming language is what developers
have been searching for. Many attempts and standards have been
created to address this very issue. This includes CORBA, COM, DCOM,
Win32 libraries, and so on. However, each solution has had a single
unavoidable roadblock: a common type system that unifies all
languages and allows them to pass information back and forth in a
consistent
|
|
|
|
|
|
|
Learning Common Types
Take an integer for example. Simple things such as the
The common type system does more than just define the size of an integer. The common type system provides three major functions:
The common type system has two classifications of types: value types and reference types. Understanding Value Types
Value types contain their data directly. In addition, value types are allocated on the runtime stack; this will become important during the discussion of the garbage collection system. Value types can be built-in, such as a char or int type, or
Table 2.1. Value Types Defined by the .NET FCL
NOTE
Value types with an asterisk in the description are not compliant with the common language specification (CLS). That means those particular types are not required or supported by all .NET-
Table source: msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconthenetframeworkclasslibrary.asp Value types created within the scope of a method exist only for the duration of that method. After the method has returned, its call stack is reclaimed and the value type no longer exists. Classes can also contain value types. In this case, the data for the value type exists for the duration of the existence of the object. Understanding Reference TypesReference types are objects that are allocated and managed by the garbage collector. Reference types are instances of classes created using the new keyword. An example would be the following: MyClass myClass = new MyClass( );
The
myClass
instance is created on the heap and managed by the garbage collector. Reference types do not disappear when they go out of scope the way that value types do. Rather, as the name implies, when all references to a reference type are gone, the garbage collector will
Reference Versus Value Types
The key difference between value type and reference type is the way they are passed to
Listing 2.1. Passing a Value Type to a Method
class MyClass {
static public void ByValue( int i) {
i = 20;
}
}
int i = 10;
Console.WriteLine( "Value of i := {0}", i );
MyClass.ByValue( i );
Console.WriteLine( "Value of i := {0}", i );
In Listing 2.1, a local variable
i
is declared and set to an initial value of 10. The
WriteLine
method displays the value to the output window.
Armed with some basic knowledge of the .NET type system, the next step is to understand how the runtime system supports these types. |
|
|
|