CLR via C # note 6,

Source: Internet
Author: User

CLR via C # note 6,
One benefit of object-oriented programming is "code reuse", which greatly improves development efficiency. If so, a class can be derived to inherit all the capabilities of the base class. The derived class can customize the behavior of the derived class by simply rewriting the virtual method or adding some new methods, to meet the needs of developers.Generic(Generic) is a special mechanism provided by CLR and programming languages. It supports code reuse in another form, that is, "algorithm reuse ". To put it simply, a developer first defines an algorithm, such as sorting, searching, exchanging, comparing, or converting. However, the developer who defines an algorithm does not set the data type to be operated by the algorithm. This algorithm can be widely applied to different types of objects. Then, another developer can use this ready-made algorithm by specifying the specific data type to be operated by the algorithm. For example, you can use a sorting algorithm to operate objects of the Int32 and String types, or use a comparison algorithm to operate objects of the DateTime and Version types. Most algorithms are encapsulated in a type. CLR allows creation of generic reference types and generic value types, but does not allow creation of generic enumeration types. You can also create generic interfaces and generic delegation. Generics provide developers with the following advantages: #1. source code protection. #2. type security. The compiler and CLR can understand the developer's intent and ensure that only objects compatible with the specified data type can be used with the algorithm. #3. Clearer code. Reduces the number of transformations that must be performed in the source code, making code easier to write and maintain. #4, better performance. Create a generic algorithm to operate on a specific value type. Therefore, a value-type instance can be passed as a value. CLR no longer needs to perform any packing operations. ArrayList to operate value types (such as Int32), will cause a large number of packing operations, a large amount of garbage collection. The most obvious application of generics is the collection class. FCL has defined several Generic collection classes. Most of these classes are in the System. Collections. Generic and System. Collections. ObjectModel namespaces.Basic Generic StructureDevelopment type and closed type we focus on how CLR creates an internal data structure for each type used by the application. This data structure is called a type object ). The type with generic type parameters is still the type, and CLR will also create an internal type object for it. This is true regardless of the reference type (class), value type (structure), interface type, or delegate type. A type with generic parameters is calledOpen Type(Open type), CLR prohibits the construction of any instance of the development type, similar to CLR prohibits the construction of an instance of the interface type, such as List <>. When the code references a generic type, you can specify a set of generic real parameters. If all types of real parameters are passed as the actual data type, the type is calledClosed Type(Closed type), CLR allows the construction of closed type instances, such as List <String>. Note that CLR allocates static fields of the type within the type object, so each closed type has its own static fields. For example, if List <T> defines any static field, it will not be shared between a List <DateTime> and a List <String>: each closed object has its own static field. If a generic type defines a static constructor, It will be executed once for each closed type. The purpose of defining a static constructor on a generic type is to ensure that the passed type real parameters meet specific conditions (constraints ). For example, if you want a generic type to only process enumeration types, you can define it as follows: internal sealed class GenericTypeThatRequiresAnEnum <T> {static GenericTypeThatRequiresAnEnum () {if (! Typeof (T ). isEnum) {throw new ArgumentException ("T must be an enmuerated type") ;}} CLR provides a function named constraint, you can use it to better define a generic type to spend which types of real parameters are valid.Code explosionWhen JIT compilation is performed using a method of generic type parameters, CLR obtains the IL of the method and replaces it with the specified type parameters, then create the appropriate local code (the code is customized for the method that specifies the data type for the operation ). To do this, the CLR must generate local code for each method/type combination. This phenomenon is calledCode explosion(Code explosion ). It may cause a significant increase in the application's working set, compromising performance. CLR actually has some built-in optimization measures to ease code explosion. If you call a method for a specific type of arguments, you can use the same type of arguments to call this method again. CLR only compiles code for this method/type combination once. If an assembly uses List <DateTime> and a completely different assembly (loaded to an AppDomain) also uses List <DateTime>, CLR only compiles the method for List <DateTime> once. This significantly relieves code explosion. CLR also provides another optimization measure, which considers all the reference type arguments to be identical, so the code can be shared. For example, the Code Compiled by CLR for the List <String> method can be directly used for the List <Stream> method, because both String and Stream are reference types. In fact, the same code is used for any reference type. The reason why CLR can perform this optimization is that all arguments or variables of the reference type actually point to the pointer of the object on the stack, and all object pointers are operated in the same way. However, if a real parameter of a type is a value type, CLR must generate local code for this value type because the value type is of an indefinite size, these values may also be manipulated using different local CPU commands.Real parameters of the Delegate and interface inverters and the wildcard type of coordination

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.