.NET generic programming introduction

zhaozj2021-02-17  88

.NET generic programming has not been far away from us, which has a generic shadow in Microsoft recently released with SQL Server Yukon Beta1. Net Framework 1.2. Although it is still a lot of problems, I believe that with the official release of the new .NET Framework (the version number at the official release is 2.0), these issues will be resolved. So we should also prepare for .NET generic programming.

The .NET system is a single inheritance system, all types are derived from Object. I have always thought that it is not used in a single inheritance system. Since all things can be passed as Object, why should I use a generic? Just increasing complexity, in addition to seeing high-profile, there seems to be no other benefit. However, when two most famous single systems, java and .net are unstoppable to join generic programming, I should inevitably want to re-examine this problem - why must I have a generic programming?

Incident, the generic ratio has the following two advantages:

1, more secure

In non-extensive programming, although all things can be passed as Object, it is unable to perform type conversion during transmission. The type conversion is unsafe at runtime. Using generic programming will reduce unnecessary type conversion to improve security.

2, higher efficiency

In non-extensive programming, simply type as Object is passed to cause boxing and unboxing operations, both of which have great overhead. Use generic programming without having to perform boxing and unboxing operations.

.NET generics have good binary reusability. This is benefited from .NET to built generics in the CLR. The Java generics in the C generic and evaluation rely on their respective compilers, and the compiler expands the exact type when compiling generic code, which inevitably occurs the code expansion. And .NET's generic code is compiled by JIT at runtime, so that the CLR can reuse most of the instant compiled code for different types.

The following two articles describe generic programming in .NET:

Introducture generics in the CLR

More on generics in the CLR

转载请注明原文地址:https://www.9cbs.com/read-31502.html

New Post(0)