C# complete reference

  • View

  • Download

Embed Size (px)



Text of C# complete reference

  • Herbert Shildt C# 2.0Complete Reference edition 2
  • Chapter 1: The Creation of C#C# is Microsofts premier language for .NET development. It leverages time-tested features withcutting-edge innovations and provides a highly usable, efficient way to write programs for themodern enterprise computing environment. It is, by any measure, one of the most importantlanguages of the 21st century.The purpose of this chapter is to place C# into its historical context, including the forces thatdrove its creation, its design philosophy, and how it was influenced by other computer languages.This chapter also explains how C# relates to the .NET Framework. As you will see, C# and the.NET Framework work together to create a highly refined programming environment.C#s Family TreeComputer languages do not exist in a void. Rather, they relate to one another, with each newlanguage influenced in one form or another by the ones that came before. In a process akin tocross-pollination, features from one language are adapted by another, a new innovation isintegrated into an existing context, or an older construct is removed. In this way, languagesevolve and the art of programming advances. C# is no exception.C# inherits a rich programming legacy. It is directly descended from two of the worlds mostsuccessful computer languages: C and C++. It is closely related to another: Java. Understandingthe nature of these relationships is crucial to understanding C#. Thus, we begin our examinationof C# by placing it in the historical context of these three languages.C: The Beginning of the Modern Age of ProgrammingThe creation of C marks the beginning of the modern age of programming. C was invented byDennis Ritchie in the 1970s on a DEC PDP-11 that used the UNIX operating system. Whilesome earlier languages, most notably Pascal, had achieved significant success, it was C thatestablished the paradigm that still charts the course of programming today.C grew out of the structured programming revolution of the 1960s. Prior to structuredprogramming, large programs were difficult to write because the program logic tended todegenerate into what is known as spaghetti code, a tangled mass of jumps, calls, and returnsthat is difficult to follow. Structured languages addressed this problem by adding well-definedcontrol statements, subroutines with local variables, and other improvements. Using structuredlanguages, it became easier to write moderately large programs.Although there were other structured languages at the time, C was the first to successfullycombine power, elegance, and expressiveness. Its terse yet easy-to-use syntax, coupled with itsphilosophy that the programmer (not the language) was in charge, quickly won many converts. Itcan be a bit hard to understand from todays perspective, but C was a breath of fresh air thatprogrammers had long awaited. As a result, C became the most widely used structuredprogramming language of the 1980s.However, even the venerable C language had its limits. Once a project reaches a certain size, itcan become difficult to understand and maintain. Precisely where this limit is reached depends
  • upon the program, the programmer, and the tools at hand, but it can be encountered with as fewas 5,000 lines of code.The Creation of OOP and C++By the late 1970s, the size of many projects was near or at the limits of what structuredprogramming methodologies and the C language could handle. To solve this problem, a new wayto program began to emerge. This method is called object-oriented programming (OOP). UsingOOP, a programmer could handle much larger programs. The trouble was that C, the mostpopular language at the time, did not support object-oriented programming. The desire for anobject-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 namewas changed to C++. C++ contains the entire C language. Thus, C is the foundation upon whichC++ 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 thefoundation of C, Stroustrup provided a smooth migration path to OOP. Instead of having to learnan entirely new language, a C programmer needed to learn only a few new features beforereaping the benefits of the object-oriented methodology.C++ simmered in the background during much of the 1980s, undergoing extensive development.By the beginning of the 1990s, C++ was ready for mainstream use, and its popularity exploded.By the end of the decade, it had become the most widely used programming language. Today,C++ is still the preeminent language for the development of high-performance system code.It is critical to understand that the invention of C++ was not an attempt to create a newprogramming language. Instead, it was an enhancement to an already highly successful language.This approach to language developmentbeginning with an existing language and moving itforwardestablished a trend that continues today.The Internet and Java EmergeThe next major advance in programming languages is Java. Work on Java, which was originallycalled Oak, began in 1991 at Sun Microsystems. The main driving force behind Javas designwas James Gosling. Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan also played arole.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 themainstreaming of the Internet, most programs were written, compiled, and targeted for a specificCPU and a specific operating system. While it has always been true that programmers like toreuse their code, finding a way to easily port a program from one environment to another took abackseat to more pressing problems. However, with the rise of the Internet, in which manydifferent types of CPUs and operating systems are connected, the old problem of portabilityreemerged with a vengeance. To solve the problem of portability, a new language was needed,and this new language was Java.
  • Although the single most important aspect of Java (and the reason for its rapid acceptance) is itsability to create cross-platform, portable code, it is interesting to note that the original impetus forJava was not the Internet, but rather the need for a platform-independent language that could beused to create software for embedded controllers. In 1993, it became clear that the issues ofcross-platform portability found when creating code for embedded controllers are alsoencountered when attempting to create code for the Internet. Remember, the Internet is a vast,distributed computing universe in which many different types of computers live. The sametechniques that solved the portability problem on a small scale could be applied to the Internet ona large scale.Java achieved portability by translating a programs source code into an intermediate languagecalled 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 theJVM is relatively easy to implement, it was readily available for a large number of environments.Javas use of bytecode differed radically from both C and C++, which were nearly alwayscompiled to executable machine code. Machine code is tied to a specific CPU and operatingsystem. Thus, if you wanted to run a C/C++ program on a different system, it needed to berecompiled to machine code specifically for that environment. Therefore, to create a C/C++program that would run in a variety of environments, several different executable versions of theprogram were needed. Not only was this impractical, it was expensive. Javas use of anintermediate language was an elegant, 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 modelis evolved from C++. Although Java code is neither upwardly nor downwardly compatible withC or C++, its syntax is sufficiently similar that the large pool of existing C/C++ programmerscould move to Java with very little effort. Furthermore, because Java built upon and improved anexisting paradigm, Gosling, et al., were free to focus their attentions on the new and innovativefeatures. Just as Stroustrup did not need to reinvent the wheel when creating C++, Gosling didnot need to create an entirely new language when developing Java. Moreover, with the creationof Java, C and C++ became the accepted substrata upon which new computer languages are built.The Creation of C#While Java has successfully addressed many of the issues surrounding portability in the Internetenvironment, there are still features that it lacks. One is cross-language interoperability, alsocalled mixed-language programming. This is the ability for the code produced by one languageto work easily with the code produced by another. Cross-language interoperability is needed forthe creation of large, distributed software systems. It is also desirable for programming softwarecomponents, because the most valuable component is one that can be used by the widest varietyof computer languages, in the greatest number of operating environments.Another feature lacking in Java is full integration with the Windows platform. Although Javaprograms can be executed in a Windows environment (assuming that the Java Virtual Machinehas been ins