C # learning Note one: CLR & C # Fundamentals

Source: Internet
Author: User

Written in the preface. Net Framework is not an abstraction layer on Win + API and COM. In a way, it is its own operating system, has its own memory manager, its own security system, its own file loader, its own error handling mechanism, its own application isolation boundary (AppDomains), its own threading model, etc. With the increasing popularity of multi-core computers, threading, concurrent execution, parallel structure, synchronization and other aspects of the importance of increasingly prominent. execution model of the CLR   Common Language RuntimeThe CLR Common Language Runtime is a runtime environment that ensures that the necessary separation between the application and the underlying operating system is the primary execution engine of the. NET Framework. is a "runtime" that can be used by multiple programming languages that target the CLR. The core capabilities of the CLR (memory management, assembly loading, security, exception handling, thread synchronization, and so on) are used by all languages that target the CLR. The CLR does not care what language the developer uses to write the source code. In other words, when choosing a programming language, you should choose the language that most easily expresses your intentions. In theory, you can write code in any language, as long as compileris for the CLR to be able. compilerA parser that can be considered a grammar checker and a "correct code". They check the source code, make sure everything you write makes sense, and then output the code that describes your intentions. Different programming languages, with different syntax. Don't underestimate the value of this choice, and perhaps save a lot of development time. Microsoft has created several language compilers for the runtime, including: C + +/CLI, C # (C sharp), Visual Basic, F #, Iron Python, Iron Ruby, and an "intermediate language" (intermediate Language, IL) assembler. IL Code (managed code)The local code compiler is code that targets a specific CPU architecture. Each CLR-oriented compiler generates a IL Code (Intermediate language Code )。 Il code is sometimes called Managed Code, because the CLR wants to manage its execution. Its obvious advantage is that it is CPU-independent. Meta DataIl code is generated by the CLR-oriented compiler, but it is not the only thing that the compiler produces to provide the runtime. The compiler also produces meta data about the original code. It gives the CLR more things about the code, such as definitions of various types, signatures of various types of members, and other data. Basically, metadata is a type library, registry content, and other information for COM. However, the metadata is merged directly with the execution code and is not in an isolated location. To put it simply, metadata is key to the entire Microsoft. NET Framework development platform, which enables seamless integration of programming languages, types, and objects. Assembly (assembly)An abstract concept. First, he is a logical grouping of one or more module/resource files. Second, the assembly is the smallest unit of reuse, security, and versioning. Depending on your choice of compiler or tool, you can generate a single-file assembly, or you can generate a multi-file assembly. In the world of the CLR, an assembly is equivalent to a "component." With the concept of "assembly", you can treat a set of files as a single entity. For a reusable, protected, versioned component, the assembly separates its logical representation from its physical representation. How you divide your code and resources into different files is entirely up to the individual. Assembly's module, and also contains information about the referenced assembly. This information enables the assembly to self-describe (self-describing). In other words, the CLR can determine what the direct dependent object (immediate dependency) of an assembly is in order to execute the code in the assembly. Managed assemblies contain both Meta DataAnd IL。 Il is a CPU-independent machine language that was painstakingly developed by Microsoft after consulting the authors of several commercial and academic languages/compilers outside. Il is more advanced than most CPU machine languages. Il can access and manipulate object types, and provides instructions to create and initialize objects, invoke virtual methods on objects, and manipulate data elements directly. You can even provide instructions for throwing and catching exceptions to implement error handling. Can be IL as an object-oriented machine languageImportant tips:Allowing for easy switching between different programming languages while maintaining tight integration is a very good feature of the CLR. Unfortunately, this feature is overlooked by many developers. For example, languages such as C # and Visual Basic can perform many I/O operations, and the APL language performs well in advanced engineering or financial calculations. Through the CLR, the I/O portion of the application can be written in C #, and the Engineering calculation section is written in APL. The CLR provides a level of integration between these languages that other technologies cannot rival, making "mixed language programming" a worthwhile choice for many development projects. Executing a method must first convert its IL to the cost of the CPU instruction, which is the responsibility of the CLR's JIT (Just-in-time or "instant") compiler. Jitcompiler is a component of the CLR, called the Jiter or JIT compiler. It looks for the il of the called Azimuth in the metadata of the assembly that defines (a type), then validates the IL Code and compiles the IL code to the cost of the CPU instruction.   The local CPU instructions are saved in a dynamically allocated block of memory. A method can only cause some performance damage on the first call. All subsequent calls to the method run at full speed in the form of native code, without having to re-validate the IL and compile it with the cost code. The JIT compiler stores the local CPU instructions in dynamic memory, and once the application terminates, the compiled code is discarded as well. So, if you run the application again in the future, or you start two instances of the application at the same time (using two different operating system processes), the JIT compiler must again compile the Il compiler cost-by-instruction. For most applications, the performance penalty caused by the JIT compiler is not significant. Most applications call the same method over and over again. These methods only have a one-time effect on performance during application runs. In addition, the time spent inside a method is likely to be much more than the time it takes to invoke the method. special attention also needs to be paid to:The CLR's JIT compiler optimizes the code, which is similar to the work done by the backend of the unmanaged C + + compiler. Similarly, it may take more time to generate optimized code. The optimized code will achieve better performance. An unmanaged meal? is compiled for a specific CPU platform, and once called, the code can be executed directly. However, in a managed environment, the compilation of code is done in two phases. First, the compiler iterates through the source code, doing as much work as possible to generate the IL code, and in order to actually execute the call, these IL codes themselves must compile the cost of the CPU instruction at run time, which requires allocating more memory and consuming additional CPU time. Practice does indicate that the second compilation phase that occurs at run time can affect performance and allocate dynamic memory. However, Microsoft has done a lot of performance tuning to keep these additional costs to a minimum. IL and ValidationIl is stack-based. Since IL does not provide instructions for manipulating registers, it is easy to create new languages and compilers to generate code that targets the CLR. The IL directive is also "untyped" (typeless). For example, IL provides an add instruction that adds the last two operands that are pressed into the stack. The add instruction is not divided into 32-bit and 64-bit versions. The highlight of IL is its abstraction of the underlying CPU, but this is not its greatest advantage. Il offers the greatest advantage of application robustness and security. When the IL compiles the cost to the CPU instruction, the CLR executes a procedure called validation (verification), which examines the advanced IL code and determines that everything that the code does is safe. For example, validation verifies that each method called has the correct number of arguments, that the arguments passed to each method have the correct type, that the return value of each method is used correctly, that each method has a return statement, and so on.  In the metadata of the managed module, contains all the method and type information to be used by the validation process. Putting each Windows process into a separate address space will gain robustness and stability, and one process cannot interfere with another process. By validating managed code, you ensure that your code does not have incorrect access to memory and does not interfere with the code of another application. In this way, you can safely put multiple managed applications into a single windoes virtual address space to run. The CLR provides the ability to execute multiple managed applications in an operating system process. Each managed application is executed in an AppDomain. By default, each managed EXE file runs in its own separate address space, which has only one AppDomain. However, the CLR's hosting process, such as IIS or MS SQL Server, can decide to run multiple AppDomain in a single operating system process. Common Type SystemThe CLR is fully expanded around the type, which should be obvious so far. Types expose functionality for applications and other types. By type, code written in one programming language can communicate with code written in another language. Because the type is fundamental to the CLR, Microsoft has specified a formal specification called a "Common type System" (Common type Systems, CTS) that describes the type definition and behavior. Using the rules specified by the CTS, the Assembly establishes a visual boundary for a type, and the CLR enforces (implements) these rules. In fact, there is no need to learn the CTS rules in this province, and the language you choose will expose its own language grammar and type rules using a formula that you are familiar with. When you build an assembly by compiling it, it maps language-specific syntax to IL-the "language" of the CLR. The CLR makes us realize: "The language of code" and "The Behavior of code".  Different languages can define the types of systems, add the same members, and have different syntax, but the behavior of the types is exactly the same, because the behavior of the type is ultimately defined by the CTS of the CLR. --------------------------------------------------------------------

C # learning Note one: CLR & C # Fundamentals

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.