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 features when you use the managed class (for example, the keyword "__gc" mentioned above).
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).
Visual J # .NET
The latest language to join this big family is Visual J # .NET. Before the .NET Framework 1.1 is launched, if you want to use J #, you must go another download. But now, J # has been built into the .NET Framework. Because of this, J # users can now have all versatile advantages of Visual Studio .NET. Microsoft I hope most J users can see if they want to write programs under .NET, J # is the most convenient choice. Unlike those languages running the environment with the Java runtime library, J # is the same as the .NET's underlying class library like .NET. This means that you can use J # to create an ASP.NET web application, Windows Forms, XMLWeb services, and all like C # and Visual Basic .NET can do.
Scripting language (scripting language)
Although it seems to be weakened due to the importance of .NET's income, they are still still use. On the other hand, JScript has now been upgraded to JScript .NET. We can now use JScript .NET to write ASP.NET programs, run JScript .NET like a compiler, rather than previous interpretation programs, and write a large number of JScript .NET code. With ASP.NET, there is no need to use scripting languages on the service period web page. However, now VBA is still used to write the macro of Microsoft Office and Visual Studio.
COM and COM
From a technical saying, COM and COM are not .NET technology, because components based on both technologies cannot be compiled into IL (although it will be possible to some extent, if the original COM component is used C is written, then managed C can be utilized). However, because some characteristics of COM are not supported by .NET, it is still an important tool. Similarly, the COM component is still able to, .NET combined with the interoperability function of COM, allowing managed code to call COM components, and vice versa. (Chapter 29 will discuss this) However, in general, it is very convenient for most purposes, such words, you can use the .NET basic class and other managed code. 1.3 Near Intermediate Language
From the chapter above, we can see that Microsoft intermediate languages are obviously playing a very important role in .NET Framework. As a C # developer, we have now understood that C # code will be compiled into IL before execution (it is true, C # is only compiled into managed code). In this way, the main characteristics of the observation of IL now have become very meaningful, because any language-oriented language also needs to support the main features of these IL.
Below is an important feature of these IL:
❑ Object-oriented and interface support
❑ 值 Value type and reference type difference is huge
❑ Strong data type
❑用 Use an exception to handle errors
❑ Property Application
Let us start at close range of property below the property.
1.3.1 Support for object-oriented and interface
The language independence of .NET has a certain actual limit. IL inevitable to achieve some specific programming methodology, this means that if there is a language ultimately compile into IL, then this language itself should be consistent with this methodology. Therefore, for IL, Microsoft selection is a classic object-oriented methodology that only supports single inheritance.
Readers who don't understand the object theory should refer to the following appendix A has obtained more information. At http://www.wrox.com you can get Appendix A.
In addition to the classic object-oriented methodology, IL also introduces the concept of an interface. The concept of this interface is COM for the first time in Windows. .NET interface and COM interface are different. However, .NET shared the interface idea of COM, the interface provides a contract, and if the class must provide this interface method and property implementation method if you want to implement a given interface.
1.3.1.1 Object-Oriented and Language Interoperability
Now we have seen that if you work and program below .NET, you must comply with classic object-oriented methodology, and the code written must also be compiled into IL. But just these are not enough to provide language interoperability. After all, C and Java also abide by the same object-oriented specification, but they still can't be considered to be interoperable. We need to further understand the concept of language interoperability.
First, we need to consider the concept of our language interoperability. After all, COM allows components to be written in different languages to cooperate together by calling each other. Is there any shortcomings? COM, through a binary standard, allowing components to instantiate other components and call their methods and properties without considering what language written in what these components are written. However, in order to achieve such purposes, each object must be instantiated by COM running and need to be accessed through an interface. Based on the thread model of the relational component, the memory space between the county is organized or the data or the data that run components or both will bring a lot of performance losses. In the extremely short case, the component resides in an executable, not a DLL file, which requires different processes to run them. It is important that components may need to communicate with each other, but they can only communicate when COM is running. Under COM, the components written in different languages cannot communicate directly, or instantiate each other, these operations are only used as the medium. Not only these, the COM architecture is not allowed to achieve inheritance, so COM has lost many object-oriented programming. Another related question is that when you debug, you still need to debug these components written in different languages separately. It is impossible to subtocate multiple languages on the debugger. So what is the true meaning of the language interoperability is a class-written class that can be directly communicated directly with the class written in another language. special:
❑ 用 Class written in a language can inherit the class written by another language
❑ A class can contain an instance of another class, not what is the language of two classes?
❑Reworthy objects can directly call objects of another language
❑ 对 Object (or reference) can pass between methods
❑ We can use step-by-step debugging when calling a method between different languages, even if this means that single-step debugging is made in the source code of different languages.
This is an absolute ambitious plan, but amazing is that .NET and IL have reached this goal. When the call between the single-step debug method is used in the debugger, it is not a CLR, but the Visual Studio .NET IDE provides us with this convenient feature.