C # note--2. Generics

Source: Internet
Author: User

Introduction to Generics:

C#2.0 the introduction of generic mechanism, it realizes the parameterization of the type and method, that is, the type becomes the logical reuse of the parameters, and a large number of type security checks are transferred from runtime to compile time, which improves the speed of code running.
Benefits of Generics:
1) Type safety
When we use generic types or generic methods to manipulate a specific data type, the compiler will ensure that these generic types or generic methods only apply to objects that are compatible with that data type, otherwise the compiler would have an error.

2) Improve performance

If there is no generic mechanism, we need to use the object type as either a parameter or a return value type, which requires a forced type conversion, and when we manipulate the object is a value type, we will need to box and unboxing the operation. After introducing a generic mechanism, we can manipulate the type by creating a generic type or generic method, eliminating the need for a forced type conversion, eliminating the need for type safety checks at run time, and eliminating the need for boxing and unpacking when manipulating value types, which improves code performance

Generic type

The generic mechanism provided by the C # language is mainly divided into
Generic types (generic types include: classes, interfaces, delegates, and structs) and generic methods

Type constraints and type inference in generics for generic interface generic delegate generic methods

C # note--2. 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.