The quest for the perfect programming language is as old as the discipline of programming itself.
In this quest, C# is the current standard bearer. Created by Microsoft to support development
for its .NET Framework, C# leverages time-tested features with cutting-edge innovations and
provides a highly usable, efficient way to write programs for the modern enterprise computing
environment. In the course of this book, you will learn to program using it.
The purpose of this chapter is to introduce C#, including the forces that drove its creation,
its design philosophy, and several of its most important features. By far, the hardest thing
about learning a programming language is the fact that no element exists in isolation. Instead,
the components of the language work together. It is this interrelatedness that makes it difficult
to discuss one aspect of C# without involving others. To overcome this problem, this chapter
provides a brief overview of several C# features, including the general form of a C# program,
two control statements, and several operators. It does not go into too many details, but rather
concentrates on the general concepts common to any C# program.
At this time, version 3.0 is the current release of C#, and this is the version taught in this
book. Of course, much of the information presented here applies to all versions of C#.
C#’s Family Tree
Computer languages do not exist in a void. Rather, they relate to one another, with each new
language influenced in one form or another by the ones that came before. In a process akin
to cross-pollination, features from one language are adapted by another, a new innovation is
integrated into an existing context, or an older construct is removed. In this way, languages
evolve and the art of programming advances. C# is no exception.
---------- Post added at 08:21 PM ---------- Previous post was at 08:20 PM ----------
C# inherits a rich programming legacy. It is directly descended from two of the world’s
most successful computer languages: C and C++. It is closely related to another: Java.
Understanding the nature of these relationships is critical to understanding C#. Thus, we begin
our examination of C# by placing it in the historical context of these three languages.
C: The Beginning of the Modern Age of Programming
The creation of C marks the beginning of the modern age of programming. C was invented by
Dennis Ritchie in the 1970s on a DEC PDP-11 that used the UNIX operating system. While
some earlier languages, most notably Pascal, had achieved significant success, it was C that
established the paradigm that still charts the course of programming today.
C grew out of the structured programming revolution of the 1960s. Structured languages are
defined by their rich set of well-designed control statements, subroutines with local variables,
code blocks, and other improvements that make it easier to organize and maintain a program.
Although other languages at the time had similar features, C implemented these elements using
a terse yet easy-to-use syntax. It also embodied a philosophy that put the programmer (not the
language) at the center of the development process. As a result, C quickly won many converts.
It became the dominant structured programming language of the late 1970s and 1980s, and it
remains so to this day.
The Creation of OOP and C++
As useful as C is, by the late 1970s, the size of many projects was near or at the limits of what
structured programming methodologies and the C language could handle. Programs were
simply becoming too large. To address this problem, a new way to program began to emerge.
This method is called object-oriented programming (OOP for short). Using OOP, a programmer
could handle much larger programs. The trouble was that C, the most popular language at the
time, did not support object-oriented programming. The desire for an object-oriented version of
C ultimately led to the creation of C++.
C++ was invented by Bjarne Stroustrup, beginning in 1979, at Bell Laboratories in Murray
Hill, New Jersey. He initially called the new language “C with Classes.” However, in 1983, the
name was changed to “C++.” C++ contains the entire C language. Thus, C is the foundation
upon which C++ is built. Most of the additions that Stroustrup made to C were designed to
support object-oriented programming. In essence, C++ is the object-oriented version of C.
By building upon the foundation of C, Stroustrup provided a smooth migration path to
OOP. Instead of having to learn an entirely new language, a C programmer needed to learn only
a few new features before reaping the benefits of the object-oriented methodology. This made
it easy for legions of programmers to make the shift from structured programming to objectoriented
programming. As a result, by the second half of the 1990s, C++ became the preeminent
language for the development of high-performance code.
It is critical to understand that the invention of C++ was not an attempt to create a new
programming language. Instead, it was an enhancement to an already highly successful
language. This approach to language development—beginning with an existing language and
moving it forward—established a trend that continues today.
---------- Post added at 08:22 PM ---------- Previous post was at 08:21 PM ----------
The Internet and Java Emerge
The next major advance in programming languages was Java. Work on Java, which was originally
called Oak, began in 1991 at Sun Microsystems. The main driving force behind Java’s design was
James Gosling. Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan also played a role.
Java is a structured, object-oriented language with a syntax and philosophy derived
from C++. The innovative aspects of Java were driven not so much by advances in the art
of programming (although some certainly were), but rather by changes in the computing
environment. Prior to the mainstreaming of the Internet, most programs were written, compiled,
and targeted for a specific CPU and a specific operating system. While it has always been
true that programmers like to reuse their code, the ability to easily port a program from one
environment to another took a backseat to more pressing problems. However, with the rise of
the Internet, in which many different types of CPUs and operating systems are connected, the
old problem of portability became substantially more important. To solve this problem, a new
language was needed, and this new language was Java.
Java achieved portability by translating a program’s source code into an intermediate
language called bytecode. This bytecode was then executed by the Java Virtual Machine (JVM).
Therefore, a Java program could run in any environment for which a JVM was available. Also,
since the JVM is relatively easy to implement, it was readily available for a large number of
environments.
In addition to the need for portability, there was a second fundamental problem that
needed to be solved before Internet-based programming could become a reality. This problem
was security. As all Internet users know, computer viruses constitute a serious and on-going
potential threat. What good would portable programs be if no one could trust them? Who
would want to risk executing a program delivered via the Internet? It might contain malicious
code. Fortunately, the solution to the security problem is also found in the JVM and bytecode.
Because the JVM executes the bytecode, it has full control of the program and can prevent a
Java program from doing something that it shouldn’t. Thus, the JVM and bytecode solved both
the issues of portability and security.
It is key to understand that Java’s use of bytecode differed radically from both C and C++,
which were nearly always compiled to executable machine code. Machine code is tied to a
specific CPU and operating system. Thus, if you wanted to run a C/C++ program on a different
system, it needed to be recompiled to machine code specifically for that environment. To create
a C/C++ program that would run in a variety of environments, several different executable
versions of the program were needed. Not only was this impractical, it was also expensive.
Java’s use of an intermediate language was an elegant and cost-effective solution. It was also
a solution that C# would adapt for its own purposes.
As mentioned, Java is descended from C and C++. Its syntax is based on C, and its
object model is evolved from C++. Although Java code is neither upwardly nor downwardly
compatible with C or C++, its syntax is sufficiently similar that the large pool of existing C/C++
programmers could move to Java with very little effort. Furthermore, because Java built upon
and improved an existing paradigm, Gosling, et al., were free to focus their attentions on the
new and innovative features. Just as Stroustrup did not need to “reinvent the wheel” when
creating C++, Gosling did not need to create an entirely new language when developing Java.