C # Study Notes 1: CLR & amp; C # basics,

Source: Internet
Author: User

C # Study Notes 1: CLR & C # basics,
Preface. Net Framework is not an abstraction layer on Win 32 API and COM. To some extent, it is its own operating system, with its own memory manager, its own security system, its own file loader, and its own error handling mechanism, application Isolation boundaries (AppDomains) and thread processing models. As multi-core computers become more common, the importance of thread processing, concurrent execution, parallel structure, synchronization, and other aspects is becoming increasingly prominent.CLR execution model Public Language RuntimeCLR Common Language Runtime is a Runtime environment that ensures necessary separation between applications and underlying operating systems. It is the main execution engine of. NET Framework. It is a "runtime" that can be used by multiple CLR-oriented programming languages ". Core CLR functions (memory management, assembly loading, security, exception handling, thread synchronization, etc.) are used by all CLR-oriented languages. CLR does not care about the language used by developers to write source code. That is to say, when selecting a programming language, you should select the language that is most likely to express your intention. Theoretically, code can be written in any language, as longCompilerIt is for CLR.CompilerIt can be regarded as a syntax checker and a parser with "correct code. They check the source code, make sure everything you write is meaningful, and then output the code that describes your intent. Different programming languages have different syntaxes. Do not underestimate the value of this choice, which may save a lot of development time. Microsoft has created several language compilers for runtime, including C ++/CLI and C # (C sharp), Visual Basic, F #, Iron Python, Iron Ruby, and an Intermediate Language (IL) assembler.IL code (managed code)The local code compiler is code for a specific CPU architecture. Each CLR-oriented compiler generatesILCode (Intermediate LanguageCode ). The IL code is sometimes calledManaged codeBecause the CLR needs to manage its execution. Its obvious advantage is that it is CPU-independent.MetadataThe IL code is generated by the CLR-oriented compiler, but it is not the only thing that the compiler generates for the runtime. The compiler also generates metadata about the original code. It provides CLR with more information about code, such as various types of definitions, signatures of various types of members, and other data. Basically, metadata is the type library, registry content, and other information used for COM. Even so, metadata is directly merged with the Execution Code and is not isolated. In short, metadata is the key to the entire microsoft. net framework development platform. It enables seamless integration of programming languages, types, and objects.Assembly)An abstract concept. First, it is a logical grouping of one or more modules/resource files. Second, an assembly is the minimum unit for reuse, security, and version control. Depending on your choice of the compiler or tool, you can generate a single file assembly or a multi-file assembly. In the CLR world, an assembly is equivalent to a "component ". With the concept of "assembly", a group of files can be treated as a separate entity. For a reusable, protected, and version-controlled component, the Assembly separates its logical representation from its physical representation. How to divide code and resources into different files depends entirely on the individual. The assembly module also contains information about the referenced assembly. This information enables the Assembly to be self-described ). In other words, CLR can determine what is the direct dependency object (immediate dependency) of an assembly in order to execute code in the Assembly. The managed Assembly also containsMetadataAndIL. IL is a machine language unrelated to CPU. It was developed by Microsoft after consulting the authors of several commercial and academic languages/compilers. IL is more advanced than most CPU machine languages. IL can access and manipulate object types, and provides instructions to create and initialize objects, call Virtual Methods on objects, and directly operate on data elements. You can even provide commands to throw and capture exceptions to handle errors. You canIL is an object-oriented machine language..Important:It allows easy switching between different programming languages while maintaining close integration. This is an outstanding feature of CLR. Unfortunately, many developers ignore this feature. For example, C # and Visual Basic can perform many I/O operations, and APL can perform advanced engineering or financial computing well. Through CLR, the I/O part of the application can be written in C #, while the engineering computing part is written in APL. CLR provides integration comparable to other technologies in these languages, making "mixed language programming" an option worth careful consideration for many development projects. To execute a method, you must first convert its IL to the local CPU command. This is the responsibility of the clr jit (just-in-time or "instant") compiler. JITCompiler is a CLR component called JITer or JIT compiler. It searches for the IL of the called location in the metadata of the defined (a type) Assembly, verifies the IL code, and compiles the IL code into the local CPU command. The local CPU command is saved to a dynamically allocated memory block. A method may cause some performance loss only when it is called for the first time. All future calls to this method will run at full speed in the form of local code, without re-Verifying the IL and compiling the local code. The JIT compiler stores local CPU commands in the dynamic memory. Once the application is terminated, the compiled code is also discarded. Therefore, if you run the application again in the future, or start two instances of the application at the same time (using two different operating system processes), the JIT compiler must re-compile the IL compilation cost command. For most applications, the performance loss caused by JIT compilers is not significant. Most applications call the same method repeatedly. During application running, these methods only affect the performance at one time. In addition, the time spent inside the method is likely to be much longer than the time spent on calling the method.Note the following:The clr jit compiler optimizes the code, which is similar to the work done by the backend of the unmanaged C ++ compiler. Similarly, it may take a lot of time to generate optimized code. The optimized code delivers better performance. An unmanaged meal is compiled for a specific CPU platform. Once called, the code can be executed directly. However, in a hosted environment, code compilation is completed in two phases. First, the compiler traverses the source code and does as much work as possible to generate the IL code. To truly execute the call, the IL Code itself must compile the CPU instructions at runtime, this requires more memory allocation and additional CPU time. Practice does show that the second compilation phase that occurs during running will affect performance and allocate dynamic memory. However, Microsoft has done a lot of performance optimization to minimize these additional overhead.IL and VerificationIL is stack-based. Because IL does not provide instructions for operation registers, it is easy to create a new language and compiler to generate code for CLR. The IL command is still "non-type" (typeless. For example, IL provides an add command to add the last two operands pushed into the stack. The add command is not 32-bit or 64-bit. The highlight of IL is its abstraction of the underlying CPU, but this is not its biggest advantage. The biggest advantage of IL is Application robustness and security. When you compile a local CPU command with IL, the CLR executes a process called verification, which checks advanced IL Code to ensure that everything the Code does is safe. For example, the verification will verify that each method called has a correct number of parameters, the parameters passed to each method have a correct type, and the return values of each method have been correctly used, each method has a return statement. The metadata of the managed module contains information about all methods and types to be used by the verification process. Every windows process is put in an independent address space, which ensures robustness and stability. One process cannot interfere with another process. By verifying the managed code, ensure that the Code does not properly access the memory and will not interfere with the code of another application. In this way, you can safely put multiple hosted applications into one elasticsearch virtual address space to run. CLR provides the ability to execute multiple hosted applications in an operating system process. Each hosted application is executed in an AppDomain. By default, each hosted exe file runs in its own independent address space, which has only one AppDomain. However, the CLR host process (such as IIS or ms SQL Server) can decide to run multiple AppDomains in a single operating system process.General Type SystemCLR is fully centered on the type, which should be obvious so far. Type: Application and other types. Through the type, the code written in one programming language can communicate with the code written in another language. Because the Type is the basis of CLR, Microsoft specifies a formal specification, called "Common Type System (CTS)", to describe the Type definition and behavior. Using the rules specified by CTS, the Assembly creates a visible boundary for a type, and the CLR enforces (enforces) These rules. In fact, you do not need to learn the CTS rules. The language you choose will use the formulas you are familiar with to disclose your own language syntax and type rules. When you compile an assembly, It maps the language-specific syntax to IL, that is, the "language" of CLR ". CLR makes us realize: "code language" and "code behavior ". Different languages can define the system type and add the same members. The syntax is different, but the type behavior is completely consistent, because the clr cts defines the type behavior. --------------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.