Introduction to C # list<t> usage

Source: Internet
Author: User
1 About the introduction

(1) Owning namespace: System.Collections.Generic

public class list<t>: Ilist<t>, Icollection<t>, Ienumerable<t>, IList, ICollection, IEnumerable.

(2) 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.

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

(4) Performance considerations: When deciding whether to use List<t> or the ArrayList class, which has similar functionality, remember that the List<t> class performs better and is type-safe in most cases.

If you use a reference type for type T of the List<t> class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.

(5) Speaking in Microsoft's words:

Any reference or value type added to ArrayList is implicitly cast upward to Object. If the item is a value type, it must be boxed when it is added to the list, and the unboxing operation is performed when it is retrieved. Both casting and boxing and unboxing operations degrade performance, and the effect of boxing and unboxing is noticeable in cases where large collections must be cycled. ”

Common methods

1 statement:

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

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> ();

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

Create a list with a collection as a parameter

String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};

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

2 adding elements:

(1) List. Add (T Item) adds an element

Mlist.add ("John");

(2) List. AddRange (ienumerable<t> collection) add a set of elements

String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};

Mlist.addrange (Temarr);

(3) Insert (int index, T item); Add an element at the index position

Mlist.insert (1, "Hei");

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);

Following

foreach (string s in mlist)

{

Console.WriteLine (s);

}

2 Deleting an element

(1) List. Remove (T Item) deletes a value

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

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

such as mlist.removeat (0);

(3) List. RemoveRange (int index, int count);

Delete count elements starting with subscript index

such as Mlist.removerange (3, 2);

3 Determine whether an element is in the list:

List. Contains (T Item) returns TRUE or FALSE, which is useful

if (Mlist.contains ("Hunter"))

{

Console.WriteLine ("There is Hunter in the list");

}

Else

{

Mlist.add ("Hunter");

Console.WriteLine ("Add Hunter successfully.");

}



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.