Further consideration of the generic set

Source: Internet
Author: User

First, preface:  

Often listen to Brother Siang the bottom of the lower level of the bottom, never seen the "bottom." Later, the sister in the project in the application of the bottom class library, from the hearing it to their own use it, only to find it is really powerful ah! Often follow the brother Siang of the class to listen to, just want to with this bottom mix a "face ripe". I also often is pretend, actually understood not how many ah. Not only the face is ripe, but also the cheek thick.

Why did you mention the generic collection again? The first contact is when the machine room is reconstructed, and the Data table is converted into a generic collection. Later, when knocking on the system, I saw that most of the returned results in the encapsulated class library were generic collections. Delegates and generics are applied, reflective and generic (something I don't understand). Generic collection application frequency is very high AH!!! Although most of the content can not understand, but listen to the effect, at least let me know more about the generic collection, but also learned a trick, using reflection to get the properties in generics.



second, the generic predecessor:  
To learn more about generic collections, it is necessary to understand its predecessors, the first is the array , the data in C # is an object, array fixed length, continuous memory, read faster, but delete the insertion speed is slow.

come again. Collection Class can be used instead of arrays to flexibly manage a bunch of objects, anything can be tolerated, in the front of the collection class, everything is the object, that is, it can save anything. Once the "any" two words are taken, we are going to go further to discuss, whether this is to be a security risk? If say, to a certain set, we store all is the int data type, because the generalization all is the object, therefore the deposit time will carry on the boxing, will transfer the stack area to the heap area, namely the int to object, in the extraction, again will dismantle the box, the packing unpacking movement inevitably brings the additional performance expense. If you do not worry about this performance problem, then because the collection does not distinguish, can store any object, then if we mix int,string or other objects in the same set, when unpacking, may be caused by improper unpacking, for example, we want to put a string "AAAAA" It is not allowed to split into an int value.




three, safe, efficient:  
See the predecessors have such limitations, the introduction of generics in Net2.0. Generics can actually be understood as a strongly typed collection class, and in ADO, there is a similar improvement, which is the introduction of a strongly typed DataSet. A generic is also a collection, but it is a set of defined storage object types that bypasses the boxing unboxing action and does not result in a unboxing error. It can be argued that the collection class is the base class part of the generic, and the newly introduced part of the generic is the polymorphic extension of the collection base class.




Four, convenient, dynamic:  
In addition to the above security, high efficiency, I also think of a generic advantage. Type parameterization, the ability to implement a generic definition of a class or method to specify a parameter, defer to the client when declaring or instantiating. That is to say this <T> is what, can define first, want to write again later. At the same time they do not need to predict how many elements to put in, their memory will automatically increase with your constant append elements.

 

I wonder if you have noticed that from the namespace of your new class,

Using System.Collections.Generic;

Microsoft has already introduced System.Collection.Generic to you by default, which means that Microsoft also wants us to use generic collections a lot. Ha ha.


Five, Summary:  
The understanding of the generic collection process from less to more, do the computer room charges, I will simply say:list<> convenient security efficiency high, now I can put these seven words a little explanation. Learning is a process of continuous deepening, never know to know a little to have a bit of their own thinking, is not a happy process it!!!




Further consideration of the generic set

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.