C # generic list)

Source: Internet
Author: User
When creating a list class, the Data Type of the list item may be int, string, or other types, but no matter what type, if the processing method for the List class is the same, there is no need to specify the Data Type in advance, and it is left to be specified when the list class is instantiated. This is equivalent to taking the data type as a parameter and can be reused to the maximum extent. Code , Protection type security and improve performance. Generic Type introduces the concept of type parameter to. NET Framework. t is usually used as a generic type parameter. Arraylist is a non-generic collection class. Any reference or value type added to arraylist is implicitly forcibly converted to an object. If the item is of the value type, you must bind it to the list and unpack it during retrieval. Another restriction is the lack of type check during compilation. Because arraylist forcibly converts all items to objects, it cannot prevent the client code from adding both the int and string types during compilation, compilation can also be passed, and errors can not be detected until the runtime. The most common use of generics is to create a collection class .. The. NET Framework class library contains several generic collection classes in the collections. Generic namespace. The list <t> class is the generic equivalent class of the arraylist class. The ilist generic interface is implemented using an array of sizes that can be dynamically increased as needed. The advantage of dynamic arrays is that you do not need to set a large array in advance to reduce unnecessary memory overhead. The C # programming guide of Microsoft msdn recommends that you use generic collection classes, such as the list <t> class, instead of non-generic collection classes, such as the arraylist class, and do not create a collection class on your own. The reason is that you do not have to do the work that the. NET Framework has completed. The Common Language Runtime Library can share the Microsoft intermediate language code and element data, which cannot be achieved by self-compiled strong types. The following example illustrates the usage of list <t>. Public class student // The student class acts as the data source {public string name {Get; set;} public list <int> scores {Get; set ;} // score set} static void main (string [] ARGs) {// initialize a list of generic classes <student>, the element in the set contains the internal Sequence List <int> List <student> students = new list <student> (); students. add (New Student {name = "James", scores = new list <int> {93, 72, 88, 78}); students. add (New Student {name = "", scores = new list <int> {95, 71, 88, 68 }}); // use LINQ to query var query = from student in students where student. scores. average ()> = 80 select new {name = student. name, score = student. scores. average ()}; foreach (var q in query) console. writeline ("{0} {1}", Q. name, Q. result);} output: Zhang San 82.75/Li Si 80.5

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.