List (of T) classes in C #

Source: Internet
Author: User
Tags mscorlib

The . Net Framework provides a generic class list for dynamic lists, this class implements the IList, ICollection, IEnumerable, IList, ICollection, IEnumerableinterface.

ICollectionInterface:

ICollectionAn interface is implemented by a generic collection class. Using this interface, you can get the number of elements in the collection (the Count property), copy the collection into the array (the CopyTo () method), and add and remove elements from the collection (add (), remove (), Clear ()).

IEnumerableInterface:

If you use a foreach statement for a collection, you need to IEnumerable the interface. This interface defines the method GetEnumerator (), which returns an enumeration that implements the IEnumerator interface.

List (of T) classes

represents a strongly typed list of objects that can be accessed through an index. Provides methods for searching, sorting, and manipulating a list.

namespaces: System.Collections.Generic

Assembly: mscorlib (in mscorlib.dll)

(1) The List (of T) class is a generic equivalent class of the ArrayList class. This class implements the IList (of T) generic interface using an array of size that can be dynamically incremented on demand.

(2) The List (of T) class uses both an equality comparer and a sort comparer.

(3) List (of T) is not guaranteed to be sorted.

(4) List (of T) accepts nothing as a valid value for a reference type and allows duplicate elements.

constructor Function:

list (Of T) Initializes a new instance of the list (of T) class that is empty and has the default initial capacity.

List (Of T) (IEnumerable (Of T) Initializes a new instance of the list (of T) class that contains elements copied from the specified collection and has sufficient capacity to accommodate the copied elements

The list (Of T) ( Int32) Initializes a new instance of the list (of T) class, which is empty and has the specified initial capacity.

1. Create a list

var intlist = new List();

var races = new List ();//Where Racer is a class

List intlist = new list (10);

Intlist.capacity = 20;

The capacity differs from the number of elements in the collection. If you have added elements to the list and do not want to add more elements, you can use the TrimExcess () method to remove the unwanted capacity. It is important to note that if the number of elements exceeds the capacity of the 90%,trimexcess () method, nothing is done.

2. Combination of the initial value setting

var intlist = new List () {1, 2};

var stringlist = new List () {"One", "one"};

3. Adding elements

3.1 List . Add Method

Syntax: public void Add (T item)

The return type of the function is void and the parameter is T.

Note the difference from Arraylist.add: The Arraylist.add parameter is of type object, so it is related to the boxing problem.

3.2 List . AddRange Method

Syntax: public void AddRange (IEnumerable collection)

4. Inserting elements

4.1 List . Insert Method

Syntax: public void Insert (int index, T item);

4.2 List . Insertrange Method

Syntax: public void insertrange (int index, IEnumerable collection)

5. Accessing elements

5.1 using indexer access

Racer r1 = racers[3];

5.2 iterating through a collection using foreach

foreach (Racer R in Racers)

{

Console.WriteLine (R);

}

5.3 ForEach () method

Performs the specified action on each element of the list, executing the action delegate on each element of the list.

Syntax: public void ForEach (action action)

static void Main ()

{

List names = new list ();

Names. ADD ("Bruce");

Names. ADD ("Alfred");

Names. ADD ("Tim");

Names. ADD ("Richard");

Display the contents of the list using the Print method.

Names. ForEach (Print);

The following demonstrates the anonymous method feature of C #

To display the contents of the list to the console.

Names. ForEach (delegate (String name)

{

Console.WriteLine (name);

});

}

private static void Print (string s)

{

Console.WriteLine (s);

}

6. Deleting an element

You can either use the index or pass the element that you want to delete.

7. Search

You can use IndexOf, LastIndexOf, and so on.

8. Sorting

Also use quick sort.

9. Performance Considerations

When deciding whether to use list or the ArrayList class, which has similar functionality, remember that the list class performs better and is type safe in most cases. If you use a reference type for the type T of the List 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.

If you use a value type for type T, the compiler generates an implementation of the List class specifically for that value type. This means that you do not have to boxing the list elements of a lists object to use the element, and after you create approximately 500 list elements, the memory saved for not boxing the list elements will be greater than the memory used to generate the class implementation.

Ensure that the value type used for type T implements the IEquatable generic interface. If not implemented, methods such as Contains must call the Object.Equals (Object) method, which boxing the affected list elements. If the value type implements the IComparable interface, and you own the source code, you should also implement the IComparable generic interface to prevent the BinarySearch and Sort methods from boxing the list elements. If you do not own the source code, pass a IComparer object to the BinarySearch and Sort methods.

It is beneficial to use the type-specific implementation of the List class instead of using the ArrayList class or writing a strongly typed wrapper collection yourself. The reason is that your implementation must do what the. NET Framework has done for you, and the common language runtime is able to share Microsoft intermediate language code and element data that your implementation cannot.

************************************************************************************************

arrays, ArrayList, list differences and use:

The capacity of an array is fixed, you can only get or set the value of one element at a time, and the capacity of the ArrayList or list automatically expands, modifies, deletes, or inserts data as needed.

An array can have multiple dimensions, and ArrayList or list< t> always have only one dimension. However, you can easily create lists of array lists or lists. The performance of arrays of specific types (except Object) is better than ArrayList. This is because the elements of ArrayList are of type Object, so boxing and unboxing operations typically occur when a value type is stored or retrieved. However, when redistribution is not required (that is, the initial capacity is very close to the maximum capacity of the list),list< t> has the same performance as an array of the same type.

When deciding whether to use list or the ArrayList class, which has similar functionality, remember that the list 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.

If you use a value type for type T, the compiler generates an implementation of the List class specifically for that value type. This means that you do not have to boxing the list elements of a lists object to use the element, and after you create approximately 500 list elements, the memory saved for not boxing the list elements will be greater than the memory used to generate the class implementation.

Ensure that the value type used for type T implements the IEquatable generic interface. If not implemented, methods such as Contains must call the Object.Equals (Object) method, which boxing the affected list elements. If the value type implements the IComparable interface, and you own the source code, you should also implement the IComparable generic interface to prevent the BinarySearch and Sort methods from boxing the list elements. If you do not own the source code, pass a IComparer object to the BinarySearch and Sort methods.

It is beneficial to use the type-specific implementation of the List class instead of using the ArrayList class or writing a strongly typed wrapper collection yourself. The reason is that your implementation must do what the. NET Framework has done for you, and the common language runtime is able to share Microsoft intermediate language code and element data that your implementation cannot.

************************************************************************************************


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

List (of T) classes in C #

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.