In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to apply C# generics". In daily operation, I believe many people have doubts about how to apply C# generics. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to apply C# generics! Next, please follow the editor to study!
When writing C # programs, we often encounter that the functions of two modules are very similar, except that one is to deal with int types, the other is to deal with string types, or other custom data types, but we have no other solution, we can only write multiple methods to deal with each data type, because the parameter types of the methods are different. So is there a way to pass in a common data type in the method so that you can merge the code? The purpose of C # generic applications is to solve this problem. What kind of problems can C# generic applications solve? Let's take a look at the following code, where something is omitted, but the function is to implement a stack that can only handle int data types:
Public class Stack {private int [] new int item; public int pop () {} public void push (int item) {} public Stack (int I) {this.m_item = new int [I];}}
The above code works fine, but what if we need a stack to hold data of type string? Perhaps many people will think of a copy of the above code copy, and change int to string. Of course, that's fine, but what if you need a stack of type long,Node in the future? Keep copying? The compromise here is to use a common data type object to implement the stack:
Public class Stack {private object [] public object pop () {} public void push () {} public Stack (int o) {this.m_item = new object [o]}}
Although this stack is flexible, it can accept any data type. But generally speaking, it is not without defects, mainly as follows:
◆ when Stack deals with value types, boxing and unboxing occurs, which allocates and reclaims a large number of variables on the managed heap. If the amount of data is large, the performance loss is very serious.
When dealing with reference types, ◆ does not have boxing and unboxing operations, but it will use data type casting operations, adding to the burden on the processor.
There are more serious problems with casting data types, as follows:
Node1 x = new Node1 (); stack.push (x); Node2 y = (Node2) stack.pop ()
There is no problem with the above code at compile time, but because push has a data of type Node1, but it is required to convert to type Node2 when pop, this will cause a type conversion exception at run time of the program, but escape the check of the compiler.
For the problem of object type stack, we introduce generics, which can solve these problems elegantly. Generics use a passed data type T to replace object, specify the type of T when the class is instantiated, and Runtime is automatically compiled to local code, which greatly improves the running efficiency and code quality, and ensures the safety of data types.
C# generic application example:
The following is to use generics to rewrite the stack above, using a common data type T as a placeholder, waiting to be replaced with an actual type when instantiated. As follows:
Public class Stack < T > {private T [] item; public T pop () {} public void push (T item) {} public Stack (int I) {this.m_item = new T [I];}}
The writing of the class remains the same, only the introduction of the general data type T can be applied to any data type, and the type is safe. The calling method of this class:
Stack < int > a = new Stack < int >; a.push (10); a.push ("10"); / / compilation fails here because class an only receives data of type int int x = a.pop (); Stack < string > b = new Stack < string > (100); b.push (10); / / compilation fails here because class b only receives database b.push of type string ("10") String y = b.pop ()
This class is quite different from the class implemented by object:
1. He's type-safe. If you instantiate a stack of type int, you cannot process data of type string, and the same is true of other data types
two。 There is no need to pack and unpack. When instantiated, this class generates native code based on the data type passed in by the lock, and the native code data type has been determined, so there is no need to box and unbox.
3. No data type conversion is required.
At this point, the study on "how to apply C# generics" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.