In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you an example analysis of the implementation of C# generics. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
C # generics implementation in .NET 2.0, C # generics have native support in IL (intermediate language) and CLR itself. When compiling normal C # server-side code, the compiler compiles it to IL, just like any other type. However, IL contains only parameters or placeholders of the actual specific type. In addition, the metadata of a general server contains general information. On the surface, the syntax of C # generics looks similar to C++ templates, but there are important differences in how the compiler implements and supports them. As you will see later, this is of great significance for the way generics are used.
Note in this article, when it comes to C++, I'm referring to traditional Clearing extensions, not Microsoft C++ with hosting extensions.
Compared with C++ templates, C# generics can provide enhanced security, but they are also limited to some extent in terms of functionality.
In some C++ compilers, the compiler does not even compile template code until you use the template class through a specific type. When you do specify a type, the compiler inserts the code inline and replaces each occurrence of a generic type parameter with the specified type. In addition, whenever you use a particular type, the compiler inserts code specific to that type, regardless of whether you have specified that type for the template class somewhere else in the application. The C++ linker is responsible for solving this problem and is not always effective. This can lead to code ballooning, which increases load time and memory footprint.
The client compiler uses this generic metadata to support type safety. When the client provides a specific type instead of a generic type parameter, the client's compiler replaces the generic type parameter in the server metadata with the specified type argument. This provides a type-specific server definition to the client-side compiler as if generics had never been involved. In this way, the client compiler can ensure the correctness of method parameters, implement type safety checks, and even perform type-specific IntelliSense.
The interesting question is how. NET compiles the general IL of the server into machine code. It turns out that the actual machine code generated depends on whether the specified type is a value type or a reference type. If the client specifies a value type, the JIT compiler replaces the general type parameter in IL with a specific value type and compiles it to native code. However, the JIT compiler keeps track of the type-specific server code it has generated. If you ask the JIT compiler to compile a generic server with the value type it has compiled into machine code, it simply returns a reference to the server code. Because the JIT compiler will use the same value type-specific server code in all future situations, there is no code ballooning problem.
If the client specifies a reference type, the JIT compiler replaces the general parameter in the server IL with Object and compiles it into native code. This code will be used in any future requests for reference types rather than generic type parameters. Note that in this way, the JIT compiler will only reuse the actual code. Instances still allocate space according to the size they leave the managed heap, and there is no cast.
Analysis of implementation examples of generics
Examples of generic methods
Static void Swap < T > (ref T a, ref T b) {Console.WriteLine ("You sent the Swap () method a {0}", typeof (T)); T temp; temp = a; a = b; b = temp;}
Generic classes, structural instances
Public class Point < T > {private T _ x; private T _ y; public T X {get {return _ x;} set {_ x = value;}} public T Y {get {return _ y;} set {_ y = value;}} public override string ToString () {return string.Format ("[{0}, {1}]", _ x, _ y) }} the above is the example analysis of the C# generics implementation shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.