Wrox's C # Advanced Programming Third Edition Part 1 (1 ~ 7)

xiaoxiao2021-03-06  99

Chapter 1.NET architecture

Read this book You will find that this book is emphasized that the C # language should not be treated by the isolated, it must be considered with .NET Framework. The C # compiler is a specific goal with .NET, this means that the code written with C # must be supported on .NET Framework. In this way, there are two important inference for C #:

❑ c C # architecture and methodology reflect the fundamental methodology of .NET.

❑ In many cases, many dedicated language features of C # depends on .NET's feature or .NET basic class.

Due to this dependency, understand the architecture and methodology of .NET's architecture and methodology before starting C # programming. This is also the purpose of this chapter.

Let's take a look at what happens when all language (including C #) is compiled and runtime. Once we have a roughly understanding, we will learn more about Microsoft Intermediate Language (MSIL or Simply IL), and all languages ​​above will eventually compile them to this assembly language. In particular, we will understand how IL, COMMON TYPE SYSTEM (CTS) and COMMON LANGUAGE SPECification (CLS) work together make the language below. Net can work together. We also have to discuss how Visual Basic and C are suitable for .NET.

After you have finished these, we will continue to learn about other features of .NET, including assemblies, namespaces, and .NET basic classes. Finally, we have to make a general understanding of the type of application that can be created like a C # developer.

1.1 C # and .NET relationship

Compared to other languages, C # is a new programming language, and the following two aspects reflect his importance:

❑ It is designed for Microsoft .NET Framework (a platform for developing, deploying, and executing distributed applications).

❑ It is a language based on modern object-oriented design method. When he is designed, the object-oriented principle has been significantly applied for more than 20 years, and Microsoft has absorbed all of these experienced language.

An important issue is to figure out that C # itself is a language. Although it is a design to generate code in the .NET environment, it is not part of .NET. There are also some features .NET support, but C # does not support, and may be more amazed, there are some features of the C # language .Net does not support! (For example, some operators overload)

However, because the C # language is used for .NET, it is important to understand the .NET Framework if we want to use C # to develop efficient software. Therefore, in this chapter, we will take some time to observe the .NET through the surface. Ok, let's get started!

1.2 Public language operation

The core of .NET Framework is a runtime execution environment, which is called the public language runtime (CLR) or .NET runtime. The code running under the control of the CLR is often referred to as a managed code.

However, all our source code (using C # or other languages) needs to be compiled before being executed by the CLR. This compilation requires two steps:

1. Compile the source code as an intermediate language (IL) code

2. Compile the intermediate language code as the code above the specific platform by the CLR

These two step compilation processes are very important because the existence of intermediate language (IL) is the key to numerous advantages of .NET. Ok, let's take a look at why.

1.2.1 Advantages of hosting code

The Microsoft Intermediate Language is shared with Java bytecode, and they are a simple low-level language (based on digital code, rather than text code), can be quickly translated into local machine code. The code has such a good design and general grammar is a significant advantage. 1.2.1.1 platform independence

First, this means that the same file containing byte code instructions can be deployed in any platform, and the last phase of the compilation process can be easily completed at runtime, so the code can run on a specific platform. In other words, the source code is compiled into an intermediate language allows us to get the .NET platform independent, which is the same as the Java platform, which is compiled as Java bytecode to obtain platform-independent.

You should notice that this platform-independent of .NET is currently theoretically, because when writing this book, only the .NET is fully implemented on the Windows platform. However, there is already a partially implemented .NET (see Mono project, one is working on .NET, for the establishment of the open source platform, please visit www.go-mono.com/)

1.2.1.2 Performance improvement

Although the front and Java have been compared, actually the intermediate language (IL) is more ambient than the Java bytecode. IL is always instant translation (called JIT compilation), but Java bytecodes are often interpretative. A disadvantage of interpreting compilation like Java is that during runtime, translate Java byte code to local executable code results in performance loss (here you do not include the newly joined Java platform, some specific Java platforms have been Realized JIT compilation).

It is not once to compile the entire application (then, the application is slower when the application is started), JIT is just a simple part of the code that will need to be used (just like his name, instant compilation). Once the code is completed, these code will be saved as the local executable result until the application exits. So, when this part of the code is called again, it is not necessary to compile again. Microsoft believes that the efficiency of this process is much higher than that compiles the entire application. Because the possibility that the application code that actually runs most of the application code is not large. With this JIT compiler, such a code will never be compiled.

This explains why we think that the hosted IL code is as fast as if you do this. However, this does not explain why Microsoft thinks we will improve performance. One reason for improvement in performance is that when the last compilation phase occurs at runtime, the JIT compiler has exactly knows the processor type that the program will run. This means that the compiler can optimize the final executable code to take advantage of any features and specific machine code instructions brought about a particular processor.

The traditional compiler also optimizes the code, but it is just an optimization that is independent of a specific processor that will run with the program. This is because the traditional compiler is compiled as a local execution code before the software is loaded. This means that the compiler does not know the processor type that the software will run, for example, is it running on the X86 compatible processor or on the alpha processor. For example, Visual Studio 6 is optimized for a normal Pentium processor, so this also means that the code it produces cannot utilize the hardware features of the Pentium III processor. Instead, JIT can be optimized in addition to the optimization work completed by Visual Studio 6, it is also possible to optimize the specific processor to run.

1.2.1.3 Language Interoperability

The role of IL is not only irrelevant to the platform, but it also promotes language interoperability. Simply put, you can compile the code of any language into IL, and this compiled code can interact with IL-compiled IL compiled into any other language. Now you may want to know what language except C # can interact with .NET, is it? Ok, then let us briefly discuss how other languages ​​make themselves .net.

Visual Basic .NET

Upgrade from Visual Basic 6 to the latest Visual Basic.Net, Visual Basic has experienced a thorough transformation. From the date of development of Visual Basic 6 in recent years, it is not a language suitable for running .NET. For example, he is integrated with COM and is just to display the source code to the developer through the program process, so that a large number of post-code developers in the source code cannot be utilized. Not only this, Visual Basic 6 does not support realization, and the standard data type of Visual Basic 6 is also incompatible with .NET.

Now, Visual Basic 6 has been upgraded to a new Visual Basic .NET, and the language changes so know, so you'd better think of Visual Basic .NET as a new programming language. The already existing Visual Basic 6 code is now compiled as code as a Visual Basic .NET. If you want to transfer your previous Visual Basic 6 code to Visual Basic .NET, you must make huge modifications. But Visual Studio .NET (a new generation of .NET-based VS) can do most of these modifications for you. If you try to open the original Visual Basic 6 in Visual Studio .Net, Visual Studio .NET will upgrade the project for you, which means it will override the original Visual Basic 6 source code with Visual Basic .ne. Although the heavy work of this upgrade code has greatly reduced, you still have to check the new Visual Basic .NET code has been determined that it is indeed correctly, after all, such a conversion may not be perfect.

Another change after the upgrade is that Visual Basic .NET will not be compiled into a local execution code, the same opposite, and C #, he is compiled into IL. Maybe you still need to continue to write Visual Basic 6 code, maybe you do need this, you must develop in the original Visual Studio 6, and such code will completely ignore the .NET Framework.

Visual C .net

In order to adapt to Windows, Microsoft Visual C 6 has made a large specific expansion. Go to Visual C .NET, which is also extended to support .NET Framework. This means that the existing C source code can be compiled as a local executable code without the continued transaction. However, this also illustrates that such local code and .NET runtime are not related. If you want to make your C code on the .NET Framework, you need to add this line to your code.

#using

You can also pass a "/ CLR" parameter to the compiler, which can set you want to compile as a managed code, compile the source code into IL instead of local machine code. Interestingly, when compiling C as a managed code, the compiler compiles an IL containing an inline execution code. This means that you can mix managed types and unmanaged types in your C code. Host C code like this: Class MyClass

{

Define a normal C class, and the following code:

__GC Class Myclass

{

Will define a hosted class, just like you are in C # or Visual Basic .NET definitions. Hosting C code is better than C # code is that you don't have to use COM interaction function to directly call the non-hosting C class.

If you try to use a .NET on the managed type, the compiler is not supported, the reporter is incorrect (for example, template or more inheritance). Of course, you will also find that you need to use non-standard C characteristics (for example, the keyword mentioned above) when you use the managed class.

Due to the freedom of C , C allows low-level pointer operations, the C compiler cannot generate code that can pass through the CLR memory type security test. If you pass through the CLR's memory type security test, you are important to your code, then you use other languages ​​to write your source code (such as C # or Visual Basic .NET).

转载请注明原文地址:https://www.9cbs.com/read-103404.html

New Post(0)