Creating C# Applications: Chapter 2

If you had a chance to design a programming language from the ground up, what would you include? Would you include support for pointers as C++ does? Or would you support the concept of automatic garbage collection, forming the basis of memory management in Java.

Perhaps you’d support the loose typing implemented in Visual Basic 6.0. Or you could opt for strong type safety, instead. Would you require that the developers pass pointers to the objects; or would you support the concept of marking a parameter as passed by reference and hence modifiable within the function?

The creators of C# at Microsoft have had a chance to look at programming language use in the last several years and ask themselves these questions. The result is an intriguing language that takes bits and pieces of several different development languages and environments and rolls them all into one.

In addition to the design of C#, the language architects also took a look at programming language interoperability, including issues such as data typing across programming languages. As a result of this effort, the Microsoft language architects participated in the design of a programming language infrastructure that supports interoperability between different programming languages, an infrastructure called the Common Language Infrastructure (CLI).

Before getting into more detail on the CLI and its impact on the design of C#, let’s take a quick look at how C# compares with three more commonly used programming languages, C++, and Visual Basic, too.

C#: A little C++, some Java, and a Touch of VB

C# owes its roots to more than one programming language. Specifically, you can see elements of three different languages in its makeup: C++, Java, and Visual Basic (though VB is a development environment and a language). Or perhaps it would be better to say that you can see the roots of programming language design that went into the composition of C++, Java, and VB in addition to other programming languages.

For instance, memory management in Java is handled by automatic garbage collection. However, automatic memory management is also used with LISP and other languages, so the use of garbage collection in C# can be attributed to LISP as much as it can be attributed to Java.

C# supports the concept of passing parameters by reference with the Ref keyword, or passing output parameters using the Out keyword. This concept is similar to the support of input/output parameter with Visual Basic. However, you can see an exact same implementation in Inprise’s Delphi product, based on Pascal. This similarity isn’t too surprising considering that one of the creators of C# is Anders Hejlsberg, formerly of Inprise.

There are also aspects of C# that are unique to it when compared with the more commonly used C++ or Java. For instance, the aspect of C# that intrigues me the most is the language’s support for unmanaged as well as managed code through the use of the unsafe keyword. Though covered in detail in the later chapter, Chapter 17, “Outside the Bounds — Memory Management, Unmanaged Code, and the Use of Unsafe”, I did want to mention it briefly now.

Using unsafe and a companion keyword, fixed, you can mark specific code within a C# application as unmanaged code. Doing this prevents automatic memory collection from occurring in this section of code. The other part of the C# application that isn’t marked as unsafe, can still be managed with automatic garbage collection. This hybrid approach to memory management allows C# developers to use language constructs such as pointers, while still taking advantage of automatic garbage collection techniques to control memory usage.

So, we can view C# as the latest culmination of several years of programming language research — at least, until another language appears on the programming language horizon.