Example usage of list in C #

Source: Internet
Author: User
Let me give you an example:

Create a WinForm solution in vs2010, and then define a class Person,person.cs code as follows:

Using system;using system.collections.generic;using system.linq;using system.text;namespace test{Public    class Person    {Public        string Name {get; set;}        public int Age {get; set;}        public string Six {get; set;}        Public DateTime Birthday {get; set;}}    }

(Mouse over the code, there will be four icons at the top of the code, the first one is to view the source code, the second is to copy the code, the third is to print the code, the fourth is to help)
Then drag in a button, double click on the button, add the code inside:

list<person> list = new list<person> (); Person person = null;for (int i = 0; i <, i++) {person    = new person ();    Person. Name = string. Format ("xxxx{0}", i);    Person. Age = + I;    Person. Birthday = DateTime.Now.AddDays (i);    Person.six = i% 2 = = 0? "Female": "Male";    List. ADD (person);} String serialstr = Jsonconvert.serializeobject (list); list<person> Listperson = new list<person> (); Listperson = jsonconvert.deserializeobject<list< Person>> (SERIALSTR); for (int i = 0; i < Listperson. Count; i++) {    MessageBox.Show (Listperson[i]. Name);}

(Mouse over the code, there will be four icons at the top of the code, the first one is to view the source code, the second is to copy the code, the third is to print the code, the fourth is to help)

The above is a simple example of list<t> usage.




C # list<t> Usage

Owning namespace: Using System.Collections.Generic;


The List<t> class is a generic equivalent class of the ArrayList class. This class implements the Ilist<t> generic interface using an array of size that can be dynamically incremented on demand.


The benefits of generics: It adds great potency and flexibility to writing object-oriented programs using the C # language. Performance is improved by not forcing boxing and unpacking of value types, or downward forcing type conversions on reference types.


First, the basis of list, common methods:


1, list<t> mlist = new list<t> ();

A.T is the element type in the list, and now takes the string type as an example

such as: list<string> mlist = new list<string> ();


B. Adding elements: List. Add (T Item) adds an element

such as: Mlist.add ("Raisine");


C. Insert element: Insert (int index, T item); Add an element at the index position

such as: Mlist.insert (1, "Laiyanbin");


D. Delete element: List. Remove (T Item) deletes a value

such as: Mlist.remove ("Raisine");


List. RemoveAt (int index); Delete the element labeled Index


such as.: Mlist.removeat (0);


List. RemoveRange (int index, int count); Delete count elements starting with subscript index

such as.: Mlist.removerange (3, 2); Error exceeding deleted range

Note: After an element is deleted, the elements following it are automatically followed


E. Determine if there is list:list. Contains (T Item) Gets the result that returns TRUE or False


F. Sort: List. Sort ()//default is the first letter of the element in ascending order


Reverses the order of the elements inside the list:

List. Reverse ()//can be associated with list. Sort () to use to achieve the desired effect


To traverse the elements in a list:

The type of the foreach (t element in Mlist) T is the same as the Mlist declaration

{

Console.WriteLine (Element);

}


G.list empty: List. Clear ()

such as: Mlist.clear ();


H. Get the number of elements in list:

List. Count () returns an int value


I. Add array into list:string[] Temarr = {Ha "," Hunter "," Tom "," Lily "," Jay "," Jim "," Kuku "," LOCU "};

Mlist.addrange (Temarr);


2, list<t> testlist =new list<t> (ienumerable<t> collection);

Create a list with a collection as a parameter

e.g.: string[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};

list<string> testlist = new list<string> (Temarr);


3, list and array of mutual conversion

1. From string[] Turn list<string>

For example: string[] str={"1", "2"};

List <string> list=new list<string> (str);

2. Transfer from list<string> to string[]


For example:list<string> list=new list<string>;

String[] Str=list. ToArray ();


viewstate["Idlist"] converted to list<>


List<int> idlist= (list<int>) viewstate["Idlist"]



Related Article

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.