In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about what C# generics is, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Generics are the new syntax introduced by Clover 2.0, not syntax candy, but 2.0 features provided by framework upgrades.
When programming programs, we often encounter modules with very similar functions, but they deal with different data. But we have no choice but to write multiple methods to deal with different data types. At this point, the question arises, is there a way to deal with passing in different types of parameters in the same way? The emergence of generics is designed to solve this problem.
Why use generics?
From the above results, we can see that the functions of all three methods are the same except for the different parameters passed in. In version 1.0, there was no concept of generics, so what to do. I'm sure many people will think of the inheritance of one of the three major features of OOP, and we know that object is the base class for all types in the C # language.
Using the Object type meets our requirements and solves the reusability of the code. Some people may ask why it is possible to import types such as int, string, etc., if the object type is defined.
There are two reasons:
1. The object type is the parent of all types.
2. Through inheritance, the subclass has all the attributes and behaviors of the parent class, and wherever the parent class appears, it can be replaced by the subclass.
But the above object-type method brings another problem: boxing and unpacking can damage the performance of the program.
Microsoft introduced generics in Craft 2.0, which can solve the above problems very well.
Generic type parameter
In a generic type or method definition, a type parameter is a placeholder for a specific type specified by the client when it instantiates a variable of the generic type. A generic class (GenericList) cannot be used as is because it is not a true type; it is more like a blueprint for a type. To use GenericList, client code must declare and instantiate the constructed type by specifying type parameters within angle brackets. The type parameter for this particular class can be any type that the compiler can recognize. You can create any number of instances of construction types, each of which uses different type parameters.
Why can generics solve the above problems?
Generics are delayed declaration: that is, no specific parameter type is specified at the time of definition, delaying the declaration of the parameter type until the time of call. The idea of delay is very popular in the design of program architecture. For example: distributed cache queue, delayed loading of EF, and so on.
After reading the above, do you have any further understanding of what C# generics are? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.