2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、<p><b>  C#</b></p><p>  A History of C, C++, and C# </p><p>  The C# programming language was created in the spirit of the C and C++ programming </p><p>  language

2、s. This accounts for its powerful features and easy learning curve. The same can't be </p><p>  said for C and C++, but because C# was created from the ground up, Microsoft took the </p><p>

3、  liberty of removing some of the more burdensome features — such as pointers. This section </p><p>  takes a look at the C and C++ languages, tracing their evolution into C#. </p><p>  The C pr

4、ogramming language was originally designed for use on the UNIX operating system. </p><p>  C was used to create many UNIX applications, including a C compiler, and was eventually </p><p>  used

5、to write UNIX itself. Its widespread acceptance in the academic arena expanded to </p><p>  include the commercial world, and software vendors such as Microsoft and Borland released </p><p>  C

6、compilers for personal computers. The original Windows API was designed to work with </p><p>  Windows code written in C, and the latest set of the core Windows operating system APIs </p><p>  r

7、emain compatible with C to this day. </p><p>  From a design standpoint, C lacked a detail that other languages such as Smalltalk had already </p><p>  embraced: the concept of an object. You

8、9;ll learn more about objects in Chapter 8, " Writing </p><p>  Object-Oriented Code." For now, think of an object as a collection of data and a set of </p><p>  operations that can be

9、 performed on that data. Object-style coding could be accomplished </p><p>  using C, but the notion of an object was not enforced by the language. If you wanted to </p><p>  structure your code

10、 to resemble an object, fine. If you didn't, fine. C really didn't care. Objects </p><p>  weren't an inherent part of the language, so many people didn't pay much attention to this </p>

11、;<p>  programming paradigm. </p><p>  After the notion of object-oriented development began to gain acceptance, it became clear that </p><p>  C needed to be refined to embrace this new

12、way of thinking about code. C++ was created to </p><p>  embody this refinement. It was designed to be backwardly compatible with C (such that all C </p><p>  programs would also be C++ programs

13、 and could be compiled with a C++ compiler). The </p><p>  major addition to the C++ language was support for this new object concept. The C++ </p><p>  language added support for classes (which

14、 are "templates" of objects), and enabled an entire </p><p>  generation of C programmers to think in terms of objects and their behavior. </p><p>  The C++ language is an improvement

15、over C, but it still has some disadvantages. C and C++ </p><p>  can be hard to get a handle on. Unlike easy-to-use languages like Visual Basic, C and C++ are </p><p>  very "low level"

16、; and require you to do a lot of coding to make your application run well. You </p><p>  have to write your own code to handle issues such as memory management and error </p><p>  checking. C an

17、d C++ can result in very powerful applications, but you need to ensure that </p><p>  your code works well. One bug can make the entire application crash or behave unexpectedly. </p><p>  Becaus

18、e of the C++ design goal of retaining backward compatibility with C, C++ was unable </p><p>  to break away from the low level nature of C. </p><p>  Microsoft designed C# to retain much of the

19、syntax of C and C++. Developers who are </p><p>  familiar with those languages can pick up C# code and begin coding relatively quickly. The </p><p>  big advantage to C#, however, is that its d

20、esigners chose not to make it backwardly </p><p>  compatible with C and C++. While this may seem like a bad deal, it's actually good news. C# </p><p>  eliminates the things that makes C an

21、d C++ difficult to work with. Because all C code is also </p><p>  C++ code, C++ had to retain all of the original quirks and deficiencies found in C. C# is </p><p>  starting with a clean slate

22、 and without any compatibility requirements, so it can retain the </p><p>  strengths of its predecessors and discard the weaknesses that made life hard for C and C++ </p><p>  programmers. <

23、/p><p>  Introducing C# </p><p>  C#, the new language introduced in the .NET Framework, is derived from C++. However, C# </p><p>  is a modern, objected-oriented (from the ground up)

24、type-safe language. </p><p>  Language features </p><p>  The following sections take a quick look at some of the features of the C# language. If some </p><p>  of these concepts do

25、n't sound familiar to you, don't worry. All of them are covered in detail in </p><p>  later chapters. </p><p><b>  Classes </b></p><p>  All code and data in C#

26、 must be enclosed in a class. You can't define a variable outside of a </p><p>  class, and you can't write any code that's not in a class. Classes can have constructors, which </p><p&

27、gt;  execute when an object of the class is created, and a destructor, which executes when an </p><p>  object of the class is destroyed. Classes support single inheritance, and all classes ultimately </p

28、><p>  derive from a base class called object. C# supports versioning techniques to help your classes </p><p>  evolve over time while maintaining compatibility with code that uses earlier versions

29、 of your </p><p><b>  classes. </b></p><p>  As an example, take a look at a class called Family. This class contains the two static fields </p><p>  that hold the first

30、 and last name of a family member as well as a method that returns the full </p><p>  name of the family member. </p><p>  class Class1 </p><p><b>  { </b></p>&l

31、t;p>  public string FirstName; </p><p>  public string LastName; </p><p>  public string FullName() </p><p><b>  { </b></p><p><b>  } </b><

32、;/p><p>  return FirstName + LastName; </p><p><b>  } </b></p><p>  Note Single inheritance means that a C# class can inherit from only one base class. </p><p>

33、;  C# enables you to group your classes into a collection of classes called a namespace. </p><p>  Namespaces have names, and can help organize collections of classes into logical groupings. </p><

34、p>  As you begin to learn C#, it becomes apparent that all namespaces relevant to the .NET </p><p>  Framework begin with System. Microsoft has also chosen to include some classes that aid in </p>

35、<p>  backwards compatibility and API access. These classes are contained within the Microsoft </p><p>  namespace. </p><p>  Data types </p><p>  C# lets you work with two typ

36、es of data: value types and reference types. Value types hold </p><p>  actual values. Reference types hold references to values stored elsewhere in memory. </p><p>  Primitive types such as cha

37、r, int and float, as well as enumerated values and structures, are </p><p>  value types. Reference types hold variables that deal with objects and arrays. C# comes with </p><p>  predefined ref

38、erence types (object and string), as well as predefined value types (sbyte, short, </p><p>  int, long, byte, ushort, uint, ulong, float, double, bool, char, and decimal). You can also define </p><

39、;p>  your own value and reference types in your code. All value and reference types ultimately </p><p>  derive from a base type called object. </p><p>  C# allows you to convert a value of o

40、ne type into a value of another type. You can work with </p><p>  both implicit conversions and explicit conversions. Implicit conversions always succeed and </p><p>  don't lose any informa

41、tion (for example, you can convert an int to a long without losing any </p><p>  data because a long is larger than an int). Explicit conversions may cause you to lose data (for </p><p>  exampl

42、e, converting a long into an int may result in a loss of data because a long can hold </p><p>  larger values than an int). You must write a cast operator into your code to make an explicit </p><p

43、>  conversion happen. </p><p>  Cross-Reference </p><p>  Refer to Chapter 3, "Working with Variables," for more information </p><p>  about implicit and explicit conve

44、rsions. </p><p>  You can work with both one-dimensional and multidimensional arrays in C#. </p><p>  Multidimensional arrays can be rectangular, in which each of the arrays has the same </p&

45、gt;<p>  dimensions, or jagged, in which each of the arrays has different dimensions. </p><p>  Classes and structures can have data members called properties and fields. Fields are </p><p&

46、gt;  variables that are associated with the enclosing class or structure. You may define a structure </p><p>  called Employee, for example, that has a field called Name. If you define a variable of type <

47、;/p><p>  Employee called CurrentEmployee, you can retrieve the employee's name by writing </p><p>  CurrentEmployee.Name. Properties are like fields, but enable you to write code to specify &l

48、t;/p><p>  what should happen when code accesses the value. If the employee's name must be read from </p><p>  a database, for example, you can write code that says, "when someone asks for

49、 the value of </p><p>  the Name property, read the name from the database and return the name as a string." </p><p>  Functions </p><p>  A function is a callable piece of cod

50、e that may or may not return a value to the code that </p><p>  originally called it. An example of a function would be the FullName function shown earlier, </p><p>  in this chapter, in the Fam

51、ily class. A function is generally associated to pieces of code that </p><p>  return information whereas a method generally does not return information. For our purposes </p><p>  however, we g

52、eneralize and refer to them both as functions. </p><p>  Functions can have four kinds of parameters: </p><p>  ? Input parameters have values that are sent into the function, but the functi

53、on cannot </p><p>  change those values. </p><p>  ? Output parameters have no value when they are sent into the function, but the function </p><p>  can give them a value and s

54、end the value back to the caller. </p><p>  ? Reference parameters pass in a reference to another value. They have a value coming </p><p>  in to the function, and that value can be changed

55、inside the function. </p><p>  ? Params parameters define a variable number of arguments in a list. </p><p>  C# and the CLR work together to provide automatic memory management. You don'

56、;t need to </p><p>  write code that says "allocate enough memory for an integer" or "free the memory that this </p><p>  object was using." The CLR monitors your memory usag

57、e and automatically retrieves more </p><p>  when you need it. It also frees memory automatically when it detects that it is no longer being </p><p>  used (this is also known as Garbage Collect

58、ion). </p><p>  C# provides a variety of operators that enable you to write mathematical and bitwise </p><p>  expressions. Many (but not all) of these operators can be redefined, enabling you t

59、o change </p><p>  how the operators work. </p><p>  C# supports a long list of statements that enable you to define various execution paths within </p><p>  your code. Flow control

60、 statements that use keywords such as if, switch, while, for, break and </p><p>  continue enable your code to branch off into different paths, depending on the values of your </p><p>  variable

61、s. </p><p>  Classes can contain code and data. Each class member has something called an accessibility </p><p>  scope, which defines the member's visibility to other objects. C# supports p

62、ublic, protected, </p><p>  internal, protected internal, and private accessibility scopes. </p><p>  Variables </p><p>  Variables can be defined as constants. Constants have value

63、s that cannot change during the </p><p>  execution of your code. The value of pi, for instance, is a good example of a constant, because </p><p>  its value won't be changing as your code r

64、uns. Enum type declarations specify a type name </p><p>  for a related group of constants. For example, you could define an enum of Planets with </p><p>  values of Mercury, Venus, Earth, Mars,

65、 Jupiter, Saturn, Uranus, Neptune and Pluto, and use </p><p>  those names in your code. Using the enum names in code makes code more readable than if </p><p>  you used a number to represent ea

66、ch planet. </p><p>  C# provides a built-in mechanism for defining and handling events. If you write a class that </p><p>  performs a lengthy operation, you may want to invoke an event when the

67、 operation is </p><p>  completed. Clients can subscribe to that event and catch the event in their code, which enables </p><p>  them to be notified when you have completed your lengthy operati

68、on. The event handling </p><p>  mechanism in C# uses delegates, which are variables that reference a function. </p><p>  Note An event handler is a procedure in your code that determines the ac

69、tions to be </p><p>  performed when an event occurs, such as the user clicking a button. </p><p>  If your class holds a set of values, clients may want to access the values as if your class we

70、re </p><p>  an array. You can write a piece of code called an indexer to enable your class to be accessed </p><p>  as if it were an array. Suppose you write a class called Rainbow, for example

71、, that contains a </p><p>  set of the colors in the rainbow. Callers may want to write MyRainbow[0] to retrieve the first </p><p>  color in the rainbow. You can write an indexer into your Rain

72、bow class to define what should </p><p>  be returned when the caller accesses your class, as if it were an array of values. </p><p>  Interfaces </p><p>  C# supports interfaces, w

73、hich are groups of properties, methods, and events that specify a set </p><p>  of functionality. C# classes can implement interfaces, which tells users that the class supports </p><p>  the set

74、 of functionality documented by the interface. You can develop implementations of </p><p>  interfaces without interfering with any existing code, which minimizes compatibility </p><p>  problem

75、s. Once an interface has been published, it cannot be changed, but it can evolve </p><p>  through inheritance. C# classes can implement many interfaces, although the classes can only </p><p>  

76、inherit from a single base class. </p><p>  Let's look at a real-world example that would benefit from interfaces to illustrate its extremely </p><p>  positive role in C#. Many applications

77、 available today support add-ins. Assume that you have </p><p>  created a code editor for writing applications. This code editor, when executed, has the </p><p>  capability to load add-ins. To

78、 do this, the add-in must follow a few rules. The DLL add-in </p><p>  must export a function called CEEntry, and the name of the DLL must begin with CEd. When </p><p>  we run our code editor,

79、it scans its working directory for all DLLs that begin with CEd. When </p><p>  it finds one, it is loaded; and then it uses the GetProcAddress to locate the CEEntry function </p><p>  within th

80、e DLL, thus verifying that you followed all the rules necessary to create an add-in. </p><p>  This method of creating and loading add-ins is very burdensome because it burdens the code </p><p>

81、  editor with more verification duties than necessary. If an interface were used in this instance, </p><p>  your add-in DLL could have implemented an interface, thus guaranteeing that all necessary </p&g

82、t;<p>  methods, properties, and events were present with the DLL itself, and functioning as </p><p>  documentation specified. </p><p>  Attributes </p><p>  Attributes decl

83、are additional information about your class to the CLR. In the past, if you </p><p>  wanted to make your class self-describing, you had to take a disconnected approach in which </p><p>  the do

84、cumentation was stored in external files such as IDL or even HTML files. Attributes </p><p>  solve this problem by enabling you, the developer, to bind information to classes — any kind </p><p>

85、;  of information. For example, you can use an attribute to embed documentation information </p><p>  into a class. Attributes can also be used to bind runtime information to a class, defining how it </p&

86、gt;<p>  should act when used. The possibilities are endless, which is why Microsoft includes many </p><p>  predefined attributes within the .NET Framework. </p><p>  Compiling C# </p

87、><p>  Running your C# code through the C# compiler produces two important pieces of </p><p>  information: code and metadata. The following sections describe these two items and then </p>&

88、lt;p>  finish up by examining the binary building block of .NET code: the assembly. </p><p>  Microsoft Intermediate Language (MSIL) </p><p>  The code that is output by the C# compiler is wr

89、itten in a language called Microsoft </p><p>  Intermediate Language, or MSIL. MSIL is made up of a specific set of instructions that </p><p>  specify how your code should be executed. It conta

90、ins instructions for operations such as </p><p>  variable initialization, calling object methods, and error handling, just to name a few. C# is </p><p>  not the only language in which source c

91、ode changes into MSIL during the compilation </p><p>  process. All .NET-compatible languages, including Visual Basic .NET and Managed C++, </p><p>  produce MSIL when their source code is compi

92、led. Because all of the .NET languages </p><p>  compile to the same MSIL instruction set, and because all of the .NET languages use the same </p><p>  runtime, code from different languages and

93、 different compilers can work together easily. </p><p>  MSIL is not a specific instruction set for a physical CPU. It knows nothing about the CPU in </p><p>  your machine, and your machine kno

94、ws nothing about MSIL. How, then, does your .NET </p><p>  code run at all, if your CPU can't read MSIL? The answer is that the MSIL code is turned into </p><p>  CPU-specific code when the

95、code is run for the first time. This process is called "just-in-time" </p><p>  compilation, or JIT. The job of a JIT compiler is to translate your generic MSIL code into </p><p>  mac

96、hine code that can be executed by your CPU. </p><p>  You may be wondering about what seems like an extra step in the process. Why generate </p><p>  MSIL when a compiler could generate CPU-spec

97、ific code directly? After all, compilers have </p><p>  always done this in the past. There are a couple of reasons for this. First, MSIL enables your </p><p>  compiled code to be easily moved

98、to different hardware. Suppose you've written some C# </p><p>  code and you'd like it to run on both your desktop and a handheld device. It's very likely that </p><p>  those two de

99、vices have different types of CPUs. If you only had a C# compiler that targeted a </p><p>  specific CPU, then you'd need two C# compilers: one that targeted your desktop CPU and </p><p>  a

100、nother that targeted your handheld CPU. You'd have to compile your code twice, ensuring </p><p>  that you put the right code on the right device. With MSIL, you compile once. Installing the </p>

101、<p>  .NET Framework on your desktop machine includes a JIT compiler that translates your MSIL </p><p>  into CPU-specific code for your desktop. Installing the .NET Framework on your handheld </p>

102、;<p>  includes a JIT compiler that translates that same MSIL into CPU-specific code for your </p><p>  handheld. You now have a single MSIL code base that can run on any device that has a .NET </p

103、><p>  JIT compiler. The JIT compiler on that device takes care of making your code run on the </p><p><b>  device. </b></p><p>  Another reason for the compiler's use

104、of MSIL is that the instruction set can be easily read by </p><p>  a verification process. Part of the job of the JIT compiler is to verify your code to ensure that </p><p>  it is as clean as

105、possible. The verification process ensures that your code is accessing memory </p><p>  properly and that it is using the correct variable types when calling methods that expect a </p><p>  spec

106、ific type. These checks ensure that your code doesn't execute any instructions that could </p><p>  make the code crash. The MSIL instruction set was designed to make this verification process </p>

107、<p>  relatively straightforward. CPU-specific instruction sets are optimized for quick execution of </p><p>  the code, but they produce code that can be hard to read and, therefore, hard to verify.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論