C # tips for using generic set-list

Source: Internet
Author: User
The most important application of the generic collection list <t> is set operations. Using a generic set can improve code reusability, type security, and better performance. The usage of list <t> is similar to that of arraylist. List <t> has better type security, and does not need to be split or packed. The syntax for defining a list <t> generic set is as follows: List <t> set name = new list <t> (); in the generic definition, the generic type parameter "<t>" must be specified. T is a placeholder for defining generic classes. t is not a type and only indicates a possible type. T will be replaced by the type used during definition. The list of generic sets can only have one parameter type. The T in "<t>" can constrain the element types in the set. Note: The generic set must be instantiated. The instantiation time is the same as that of the common class. You must add "()" to the end. For example, define a list of student classes, for example: List <student> students = new list <student> (); list <t> the method of adding, deleting, and retrieving elements is similar to that of arraylist. It is obvious that it does not need to be packed or split like arraylist. Using system; using system. collections. generic; using system. collections; public class student {public string name {Get; set;} Public String number {Get; set;} public int score {Get; Set ;}} class program {static void main () {list <student> students = new list <student> (); Student stu1 = new student (); stu1.name = "Lu Xiaofeng "; stu1.number = "0801"; stu1.score = 20; Student stu2 = new student (); stu2.name =" Simon blow snow "; stu2.number =" 0802 "; stu2.score = 23; students. add (stu1); students. add (stu2); console. writeline ("the number of elements in the set is {0}", students. count); foreach (student Stu in students) {console. writeline ("\ t {0} \ t {1} \ t {2}", Stu. name, Stu. number, Stu. score);} students. remove (stu1); console. writeline ("the number of elements in the set is {0}", students. count); console. readline () ;}} the code above defines the list <t> generic set of the student type, where T is replaced by the Data Type student, so that the defined set can only be saved The student type elements are saved to ensure the type security. When traversing the collection elements, they are not removed from the bin, improving the performance. Differences between list <t> and arraylist <t> the similarities between list <t> and arraylist are as follows: adding, deleting, and accessing elements through indexes are the same. List <t> differs from arraylist: arraylist can be used to add any type of elements. List <t> has type constraints on the added elements. arratlist can be used to add a fashion box and split it when reading data; list <t> unboxing is not required. Let's look at another example: using system; using system. collections; using system. collections. generic; class person {private string _ name; // name private int _ age; // age // create person object public person (string name, int age) {This. _ name = Name; this. _ age = age;} // name public string name {get {return _ name ;}// age public int age {get {r Eturn _ age ;}} class program {static void main () {// create the person object person p1 = new person ("James", 30 ); person P2 = new person ("Li Si", 20); person P3 = new person ("Wang Wu", 50 ); // create a list of objects with the person type <person> Persons = new list <person> (); // put the person object in the Set persons. add (P1); persons. add (P2); persons. add (P3); // output the name of 2nd people. writeline (persons [1]. name); foreach (person P in persons) {console. writeline ("\ T {0} \ t {1} ", p. name, P. age) ;}}you can see that the generic set greatly simplifies the implementation code of the Set, through which you can easily create a set of the specified type. In addition, generic collections provide more powerful functions. Let's take a look at the sorting and search functions. The following uses generics to load the data of the combox control: public class vendor {private string _ usercode; private string _ vendordes; Public String vendordes {get {return _ vendordes ;} set {_ vendordes = value ;}} Public String usercode {get {return _ usercode;} set {_ usercode = value ;}} public vendor (string display, string value) {_ vendordes = display; _ usercode = value;} public vendor () {}}/// <summary> /// obtain the Supplier name // </Summary> /// <Param name = "SQL"> </param> /// <returns> </returns> public list <model. vendor> getvendlist () {list <model. vendor> lstv = new list <model. vendor> (); string SQL = string. format ("Select Code, vendordes from vendor"); Using (sqldatareader DR = sqlhelper. executereader (sqlhelper. conn, commandtype. text, SQL) {While (dr. read () {lstv. add (new model. vendor (Dr ["code"]. tostring (). trim (), Dr ["vendordes"]. tostring (). trim () ;}} return lstv ;}// test combox BLL. po Vd = new BLL. po (); List <model. vendor> lst = new list <model. vendor> (); lst = VD. getvendlist (); foreach (model. vendor V in lst) {string code = v. usercode; string vendor = v. vendordes; combobox1.items. add (vendor); combobox1.displaymember = Code; combobox1.valuemember = vendor ;}

Link: http://www.code-design.cn/article/20120210/csharp-generic-list-object.aspx

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.