1、4856单词,8230汉字C# A History of C, C+, and C# The C# programming language was created in the spirit of the C and C+ programming languages. This accounts for its powerful features and easy learning curve. The same can't be said for C and C+, but because C# was created from t
2、he ground up, Microsoft took the liberty of removing some of the more burdensome features such as pointers. This section takes a look at the C and C+ languages, tracing their evolution into C#. The C programming language was originally designed for use on the UNIX operating system
3、. C was used to create many UNIX applications, including a C compiler, and was eventually used to write UNIX itself. Its widespread acceptance in the academic arena expanded to include the commercial world, and software vendors such as Microsoft and Borland released C compile
4、rs for personal computers. The original Windows API was designed to work with Windows code written in C, and the latest set of the core Windows operating system APIs remain compatible with C to this day. From a design standpoint, C lacked a detail that other languages such as Smal
5、ltalk had already embraced: the concept of an object. You'll learn more about objects in Chapter 8, " Writing Object-Oriented Code." For now, think of an object as a collection of data and a set of operations that can be performed on that data. Object-style coding cou
6、ld be accomplished using C, but the notion of an object was not enforced by the language. If you wanted to structure your code to resemble an object, fine. If you didn't, fine. C really didn't care. Objects weren't an inherent part of the language, so many
7、 people didn't pay much attention to this programming paradigm. After the notion of object-oriented development began to gain acceptance, it became clear that C needed to be refined to embrace this new way of thinking about code. C+ was created to embody this refinement.
8、 It was designed to be backwardly compatible with C (such that all C programs would also be C+ programs and could be compiled with a C+ compiler). The major addition to the C+ language was support for this new object concept. The C+ language added support for classes (which are &qu
9、ot;templates" of objects), and enabled an entire generation of C programmers to think in terms of objects and their behavior. The C+ language is an improvement over C, but it still has some disadvantages. C and C+ can be hard to get a handle on. Unlike easy-to-use languages l
10、ike Visual Basic, C and C+ are very "low level" and require you to do a lot of coding to make your application run well. You have to write your own code to handle issues such as memory management and error checking. C and C+ can result in very powerful applications, but y
11、ou need to ensure that your code works well. One bug can make the entire application crash or behave unexpectedly. Because of the C+ design goal of retaining backward compatibility with C, C+ was unable to break away from the low level nature of C. Microsoft designed C# to r
12、etain much of the syntax of C and C+. Developers who are familiar with those languages can pick up C# code and begin coding relatively quickly. The big advantage to C#, however, is that its designers chose not to make it backwardly compatible with C and C+. While this may seem like
13、 a bad deal, it's actually good news. C# eliminates the things that makes C and C+ difficult to work with. Because all C code is also C+ code, C+ had to retain all of the original quirks and deficiencies found in C. C# is starting with a clean slate and without any compatibilit
14、y requirements, so it can retain the strengths of its predecessors and discard the weaknesses that made life hard for C and C+ programmers. Introducing C# C#, the new language introduced in the .NET Framework, is derived from C+. However, C# is a modern, objected-orie
15、nted (from the ground up) type-safe language. Language features The following sections take a quick look at some of the features of the C# language. If some of these concepts don't sound familiar to you, don't worry. All of them are covered in detail in later chapte
16、rs. Classes All code and data in C# must be enclosed in a class. You can't define a variable outside of a class, and you can't write any code that's not in a class. Classes can have constructors, which execute when an object of the class is created, and a destru
17、ctor, which executes when an object of the class is destroyed. Classes support single inheritance, and all classes ultimately derive from a base class called object. C# supports versioning techniques to help your classes evolve over time while maintaining compatibilit
18、y with code that uses earlier versions of your classes. As an example, take a look at a class called Family. This class contains the two static fields that hold the first and last name of a family member as well as a method that returns the full name of the family member. &n
19、bsp; class Class1 public string FirstName; public string LastName; public string FullName() return FirstName + LastName; Note Single inheritance means that a C# class can inherit from only one base class. C# enables you to group your c
20、lasses into a collection of classes called a namespace. Namespaces have names, and can help organize collections of classes into logical groupings. As you begin to learn C#, it becomes apparent that all namespaces relevant to the .NET Framework begin with System. Microsoft has also
21、 chosen to include some classes that aid in backwards compatibility and API access. These classes are contained within the Microsoft namespace. Data types C# lets you work with two types of data: value types and reference types. Value types hold actual values. Referen
22、ce types hold references to values stored elsewhere in memory. Primitive types such as char, int and float, as well as enumerated values and structures, are value types. Reference types hold variables that deal with objects and arrays. C# comes with predefined reference types (obje
23、ct and string), as well as predefined value types (sbyte, short, int, long, byte, ushort, uint, ulong, float, double, bool, char, and decimal). You can also define your own value and reference types in your code. All value and reference types ultimately derive from a base type call
24、ed object. C# allows you to convert a value of one type into a value of another type. You can work with both implicit conversions and explicit conversions. Implicit conversions always succeed and don't lose any information (for example, you can convert an int to a long without
25、 losing any data because a long is larger than an int). Explicit conversions may cause you to lose data (for example, converting a long into an int may result in a loss of data because a long can hold larger values than an int). You must write a cast operator into your code to make
26、 an explicit conversion happen. Cross-Reference Refer to Chapter 3, "Working with Variables," for more information about implicit and explicit conversions. You can work with both one-dimensional and multidimensional arrays in C#. Multidimensional arrays
27、can be rectangular, in which each of the arrays has the same dimensions, or jagged, in which each of the arrays has different dimensions. Classes and structures can have data members called properties and fields. Fields are variables that are associated with the enclosing class or structure. You may define a structure called Employee, for example, that has a field called Name. If you define a variable of type Employee called CurrentEmployee, you can retrieve the employee's name by writing