Tuesday, March 31, 2009

The Philosophy of .NET

Every few years or so, the modern-day programmer must be willing to perform a self-inflicted knowledge
transplant to stay current with the new technologies of the day. The languages (C++, Visual
Basic 6.0, Java), frameworks (MFC, ATL, STL), and architectures (COM, CORBA, EJB) that were touted
as the silver bullets of software development eventually become overshadowed by something better or
at the very least something new. Regardless of the frustration you can feel when upgrading your internal
knowledge base, it is unavoidable. The .NET platform is Microsoft’s current offering within the landscape
of software engineering.
The point of this chapter is to lay the conceptual groundwork for the remainder of the book. It
begins with a high-level discussion of a number of .NET-related topics such as assemblies, the common
intermediate language (CIL), and just-in-time (JIT) compilation. In addition to previewing
some key features of the C# programming language, you will also come to understand the relationship
between various aspects of the .NET Framework, such as the common language runtime (CLR),
the Common Type System (CTS), and the Common Language Specification (CLS). As you would hope,
all of these topics are explored in further detail throughout the remainder of this text.
This chapter also provides you with an overview of the functionality supplied by the .NET base
class libraries, sometimes abbreviated as the “BCL” or alternatively as the “FCL” (being the Framework
class libraries). Finally, this chapter investigates the language-agnostic and platform-independent
nature of the .NET platform (yes it’s true, .NET is not confined to the Windows operating system).

Understanding the Previous State of Affairs
Before examining the specifics of the .NET universe, it’s helpful to consider some of the issues that
motivated the genesis of Microsoft’s current platform. To get in the proper mind-set, let’s begin this
chapter with a brief and painless history lesson to remember our roots and understand the limitations
of the previous state of affairs (after all, admitting you have a problem is the first step toward
finding a solution). After completing this quick tour of life as we knew it, we turn our attention to
the numerous benefits provided by C# and the .NET platform.

Life As a C/Win32 API Programmer
Traditionally speaking, developing software for the Windows family of operating systems involved
using the C programming language in conjunction with the Windows application programming
interface (API). While it is true that numerous applications have been successfully created using this
time-honored approach, few of us would disagree that building applications using the raw API is
a complex undertaking.
The first obvious problem is that C is a very terse language. C developers are forced to contend
with manual memory management, ugly pointer arithmetic, and ugly syntactical constructs. Furthermore,
given that C is a structured language, it lacks the benefits provided by the object-oriented
approach (can anyone say spaghetti code?) When you combine the thousands of global functions
and data types defined by the Win32 API to an already formidable language, it is little wonder that
there are so many buggy applications floating around today.

Life As a C++/MFC Programmer
One vast improvement over raw C/API development is the use of the C++ programming language.
In many ways, C++ can be thought of as an object-oriented layer on top of C. Thus, even though
C++ programmers benefit from the famed “pillars of OOP” (encapsulation, inheritance, and polymorphism),
they are still at the mercy of the painful aspects of the C language (e.g., manual memory
management, ugly pointer arithmetic, and ugly syntactical constructs).
Despite its complexity, many C++ frameworks exist today. For example, the Microsoft
Foundation Classes (MFC) provides the developer with a set of C++ classes that facilitate the
construction of Win32 applications. The main role of MFC is to wrap a “sane subset” of the raw
Win32 API behind a number of classes, magic macros, and numerous code-generation tools
(aka wizards). Regardless of the helpful assistance offered by the MFC framework (as well as many
other C++-based windowing toolkits), the fact of the matter is that C++ programming remains
a difficult and error-prone experience, given its historical roots in C.

Life As aVisual Basic 6.0 Programmer
Due to a heartfelt desire to enjoy a simpler lifestyle, many programmers have shifted away from the
world of C(++)-based frameworks to kinder, gentler languages such as Visual Basic 6.0 (VB6). VB6 is
popular due to its ability to build complex user interfaces, code libraries (e.g., COM servers), and
data access logic with minimal fuss and bother. Even more than MFC, VB6 hides the complexities of
the raw Win32 API from view using a number of integrated code wizards, intrinsic data types, classes,
and VB-specific functions.
The major downfall of VB6 (which has been rectified given the advent of Visual Basic .NET) is
that it is not a fully object-oriented language; rather, it is “object aware.” For example, VB6 does not
allow the programmer to establish “is-a” relationships between types (i.e., no classical inheritance)
and has no intrinsic support for parameterized class construction. Moreover, VB6 doesn’t provide
the ability to build multithreaded applications unless you are willing to drop down to low-level
Win32 API calls (which is complex at best and dangerous at worst).

Monday, March 30, 2009

Introduction

Iremember a time years ago when I proposed a book to Apress regarding a forthcoming software SDK
code-named Next Generation Windows Services (NGWS). As you may be aware, NGWS eventually
became what we now know as the .NET platform. My research of the C# programming language and
the .NET platform took place in parallel with the authoring of the initial manuscript. It was a fantastic
project; however, I must confess that it was more than a bit nerve-racking writing about a technology
that was undergoing drastic changes over the course of its development. Thankfully, after many
sleepless nights, the first edition of C# and the .NET Platform was published in conjunction with the
release of .NET 1.0 Beta 2, circa the summer of 2001.
Since that point, I have been extremely happy and grateful to see that this text was very well
received by the press and, most important, by readers. Over the years it was nominated as a Jolt
Award finalist (I lost . . . crap!) and for the 2003 Referenceware Excellence Award in the programming
book category (I won? Cool!).
The second edition of this text (C# and the .NET Platform, Second Edition) provided me the
opportunity to expand upon the existing content with regard to version 1.1 of the .NET platform.
Although the second edition of the book did offer a number of new topics, a number of chapters and
examples were unable to make it into the final product.
Now that the book has entered its third edition, I am happy to say that the manuscript contains
(almost) all of the topics and examples I was unable to cram into the previous versions. Not only
does this edition of the text account for the numerous bells and whistles brought about by .NET 2.0
but it also incorporates a number of chapters that have long been written but not yet published
(such as content on the common intermediate language, or CIL).
As with the earlier editions, this third edition presents the C# programming language and .NET
base class libraries using a friendly and approachable tone. I have never understood the need
some technical authors have to spit out prose that reads more like a GRE vocabulary study guide than
a readable book. As well, this new edition remains focused on providing you with the information
you need to build software solutions today, rather than spending too much time examining esoteric
details that few individuals will ever actually care about.
We’re a Team, You and I
Technology authors write for a demanding group of people (I should know—I’m one of them). You
know that building software solutions using any platform is extremely detailed and is very specific
to your department, company, client base, and subject matter. Perhaps you work in the electronic
publishing industry, develop systems for the state or local government, or work at NASA or a branch
of the military. Speaking for myself, I have developed children’s educational software, various n-tier
systems, and numerous projects within the medical and financial industries. The chances are almost
100 percent that the code you write at your place of employment has little to do with the code I write
at mine (unless we happened to work together previously!).
Therefore, in this book, I have deliberately chosen to avoid creating examples that tie the
example code to a specific industry or vein of programming. Given this, I choose to explain C#, OOP,
the CLR, and the .NET 2.0 base class libraries using industry-agnostic examples. Rather than having
every blessed example fill a grid with data, calculate payroll, or whatnot, I’ll stick to subject matter we
can all relate to: automobiles (with some geometric structures and employees thrown in for good
measure). And that’s where you come in.
My job is to explain the C# programming language and the core aspects of the .NET platform
the best I possibly can. As well, I will do everything I can to equip you with the tools and strategies
you need to continue your studies at this book’s conclusion.
Your job is to take this information and apply it to your specific programming assignments.
I obviously understand that your projects most likely don’t revolve around automobiles with pet
names, but that’s what applied knowledge is all about! Rest assured, once you understand the concepts
presented within this text, you will be in a perfect position to build .NET solutions that map to
your own unique programming environment.