When defining a generic type or method, any variables it specifies for types (such as T) are called type parameters. Example is the List class definition, indexer method.
Source code protection - The developer using a generic algorithm doesn’t need to have access to the algorithm’s source code. With C++ templates or Java’s generics, however, the algorithm’s source code must be available to the developer who is using the algorithm.
Type safety - When a generic algorithm is used with a specific type, the compiler and the CLR understand this and ensure that only objects compatible with the specified data type are used with the algorithm. Attempting to use an object of an incompatible type will result in either a compiler error or a runtime exception being thrown.
Cleaner code - Since the compiler enforces type safety, fewer casts are required in your source code, meaning that your code is easier to write and maintain.
Better performance - Before generics, the way to define a generalized algorithm was to define all of its members to work with the Object data type. If you wanted to use the algorithm with value type instances, the CLR had to box the value type instance prior to calling the members of the algorithm. Since a generic algorithm can now be created to work with a specific value type, the instances of the value type can be passed by value, and the CLR no longer has to do any boxing. In addition, since casts are not necessary the CLR doesn’t have to check the type safety of the attempted cast, and this results in faster code too.
using the generic List algorithm with the Int32 type is much. faster than using the non-generic ArrayList algorithm with Int32. List algorithm required 0 garbage collections. Generic List algorithm is of any benefit here. Using reference types you do need to realize that the CLR generates native code for each method the first time the method is called for a particular data type.
CLR creates an internal data structure for each and every type in use by an application. These data structures are called type objects. A type with generic type parameters is called an open type, and the CLR does not allow any instance of an open type to be constructed (similar to how the CLR prevents an instance of an interface type from being constructed)
When a method that uses generic type parameters is JIT-compiled, the CLR takes the method’s IL, substitutes the specified type arguments, and then creates native code that is specific to that method operating on the specified data types. This is exactly what you want and is one of the main features of generics. However, there is a downside to this: the CLR keeps
generating native code for every method/type combination. This is referred to as code explosion. CLR has some optimizations - the CLR will compile the code for this method/type combination just once.
The following are the key differences between C# Generics and C++ templates:
•C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators.
•C# does not allow non-type template parameters, such as template C
•C# does not support explicit specialization; that is, a custom implementation of a template for a specific type.
•C# does not support partial specialization: a custom implementation for a subset of the type arguments.
•C# does not allow the type parameter to be used as the base class for the generic type.
•C# does not allow type parameters to have default types.
•In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters.
•C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints.
You can think of templates as a fancy-pants search-and-replace mechanism. When you say DoIt
That’s not how generic types work; generic types are, well, generic. We do the overload resolution once and bake in the result. We do not change it at runtime when someone, possibly in an entirely different assembly, uses string as a type argument to the method. The IL we’ve generated for the generic type already has the method its going to call picked out.
No comments:
Post a Comment