CLR via C # Deep solution note six-generics

Source: Internet
Author: User

One of the benefits of object-oriented programming is "code reuse", which greatly improves development efficiency. If so, you can derive a class that inherits all the capabilities of the base class, and the derived class simply needs to override the virtual method, or add some new methods to customize the behavior of the derived class to meet the needs of the developer. generic type(generic) is a special mechanism provided by the CLR and the programming language that supports another form of code reuse, the "algorithm reuse". Simply put, developers define good one algorithms, such as sorting, searching, swapping, comparing, or converting. However, the developer who defines the algorithm does not set what data type the algorithm will manipulate, and the algorithm can be widely applied to different types of objects. Then another developer can use this out-of-the-box algorithm as long as it specifies the specific data type to be manipulated by the algorithm. For example, you can use a sort algorithm to manipulate objects of types such as Int32 and string, or to manipulate objects of types such as datetime and version with a comparison algorithm. Most algorithms are encapsulated in one type, and the CLR allows the creation of generic reference types and generic value types, but does not allow the creation of generic enumeration types. Also allows you to create generic interfaces and generic delegates. Generics provide developers with the advantage of #1, source code protection. #2, type safe. The compiler and the CLR can understand the intent of the developer and ensure that only objects that are compatible with the specified data type can be used with the algorithm. #3, clearer code. Reduces the number of transitions that must be made in the source code, which is easier to write and maintain. #4, better performance. Creates a generic algorithm to manipulate a specific value type, so an instance of a value type can be passed as a value, and the CLR no longer needs to perform any boxing operations. ArrayList to manipulate value types (such as Int32) can result in a large number of boxing operations and a large amount of garbage collection. The most obvious application of generics is the collection class. The FCL already defines several generic collection classes, where most of the classes are in the System.Collections.Generic and System.Collections.ObjectModel namespaces. Generic InfrastructureDevelopment types and enclosing types we are concerned about how the CLR creates an internal data structure for each type used by the application, which is called the type object. A type with a generic type parameter is still a type, and the CLR also creates an intrinsic type object for it. This is true regardless of the reference type (Class), value type (struct), interface type, or delegate type. Types with generic type parameters are called Open Type(open type), the CLR prohibits constructing any instance of the development type, similar to the CLR, which prohibits constructing an instance of an interface type, such as list<>. When the code references a generic type, you can specify a set of generic type arguments. If the actual data type is passed for all type arguments, the type is called closed Type(closed type), the CLR allows you to construct an instance of a enclosing type, such as list<string>. It is important to note that the CLR allocates static fields of types within the type object, so each enclosing type has its own static field. For example,,list<t> defines any static fields that are not shared between a list<datetime> and a list<string>: Each enclosing type object has its own static field. If a generic type defines a static constructor, the constructor executes once for each enclosing type. The purpose of defining a static constructor on a generic type is to ensure that the passed type arguments satisfy a specific condition (constraint). For example, if you want a generic type to handle only the enumeration type, you can define it as follows: internal sealed class Generictypethatrequiresanenum<t> {static Generictypethatrequiresanenum () {if (!typeof (T).          Isenum) {throw new ArgumentException ("T must is an enmuerated type"); }}} The CLR provides a feature called constraints that can be used to better define a generic type to spend on which type arguments are valid. Code ExplosionWhen a method that uses a generic type parameter is JIT-compiled, the CLR obtains the Il of the method, replaces it with the specified type arguments, and then creates the appropriate local code (which is "tailored" to the method that specifies the data type for the operation). This has one drawback: the CLR generates local code for each combination of different method/type combinations. This phenomenon is called Code Explosion(Code explosion). It can cause a significant increase in the working set of the application, thereby compromising performance. The CLR actually built some optimizations to mitigate the code explosion. If a method is called for a particular type argument, the method is called again later using the same type argument. The CLR compiles code only once for this method/type combination. If an assembly uses list<datetime> a completely different assembly (loaded into an AppDomain) also uses list<datetime> the CLR will only list<datetime> Compile the method once. This significantly eases the code explosion. The CLR also provides another optimization measure, which considers that all reference type arguments are identical, so the code can be shared. For example, code compiled by the CLR for list<string> methods can be used directly with the List<stream> method, because both String and Stream are reference types. In fact, the same code will be used for any reference type. The CLR is able to perform this optimization because the arguments or variables of all reference types are actually pointers to objects on the heap, and the object pointers are all manipulated in the same way. However, a type argument is a value type, and the CLR must generate local code specifically for that value type, because the value type is variable in size and may be manipulated with different local CPU instructions. contravariant and covariant generic type arguments for delegates and interfaces

CLR via C # Deep solution note six-generics

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.