SoFunction
Updated on 2025-03-07

Introduction to List and SortedList in C#

1. List introduction

Namespace to which it belongs:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

The List<T> class is a generic equivalent class of the ArrayList class. This class implements the IList<T> generic interface using an array with dynamically increasing size on demand.

Benefits of generics: It adds great effectiveness and flexibility to writing object-oriented programs in C# language. The value type is not forcibly boxed and unboxed, or down-casted reference types are cast, so performance is improved.

2. Performance precautions:

When deciding whether to use IList<T> or ArrayList classes (both have similar functionality), remember that the IList<T> class performs better and is type-safe in most cases.

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

"Any reference or value type added to an ArrayList will be implicitly cast upwards to an Object. If an item is a value type, it must be boxed when it is added to the list and unboxing when it is retrieved. Casting, as well as boxing and unboxing operations, all degrade performance; the effects of boxing and unboxing are very obvious when a large collection must be circulated."

3. General usage

1. Basics and common methods of List:

Statement:

List&lt;T&gt; mList = new List&lt;T&gt;(); 
//T is the element type in the list, now take the string type as an exampleList&lt;string&gt; mList = new List&lt;string&gt;(); 
List&lt;T&gt; testList =new List&lt;T&gt; (IEnumerable&lt;T&gt; collection);
//Create a List with a collection as a parameterstring[] temArr = { "Ha", "Hunter",};
List&lt;string&gt; testList = new List&lt;string&gt;(temArr);

Add elements:

  1. List. Add(T item)   Add an element
  2. List. AddRange(IEnumerable<T> collection)   Add a set of elements
  3. Insert(int index, T item);  Add an element in the index position

Traversing elements in List:

foreach (T element in mList) TTypes andmListSame as when declared
 {
    (element);
 }

Delete elements:

  1. List. Remove(T item)       Delete a value
  2. List. RemoveAt(int index);   Delete the element with index subscript
  3. List. RemoveRange(int index, int count); Start with the index index and delete count elements

Determine whether an element is in the List: List. Contains(T item) Return true or false, which is very practical

Sort the elements in List: List. Sort ()   By default, the first letter of the element is in ascending order

Reverse the order of elements in List: List. Reverse ()   Can be used in conjunction with List. Sort () to achieve the desired effect

List clear: List. Clear ()

Get the number of elements in List: List. Count ()  Returns the int value

The SortedList class represents a collection of key-value pairs sorted by keys and accessed by keys and indexes.

A sorted list is a combination of arrays, hash tables. It contains a list of items that can be accessed using keys or indexes. If you use an index to access the item, this is an ArrayList, and if you use one-click access to the item, this is a Hashtable. The items of the collection are always sorted by key values.

Methods and properties of SortedList class

The following table lists some common properties for sorted list classes:

property describe
Capacity Get or set the capacity of the sorted list
Count Get the number of elements contained in the sorted list
IsFixedSize Gets a value indicating whether the sorted list has a fixed size
IsReadOnly Gets a value indicating whether the sorted list is read-only
Item Get and set the value associated with a specific key in the SordDead list
Keys Get the key of the sorted list
Values Get the value in the sorted list (SortedList)

The following table lists some common methods of sorted list (SortedList) classes:

  • void Add( object key, object value);   Put elements with the specified key and value to the sorted list
  • public virtual void Clear();   will delete all elements of SortedList
  • public virtual bool ContainsKey( object key);   Determine whether a specific key is contained in the SortedList
  • public virtual bool ContainsKey( object key);   Determine whether a specific key is contained in the SortedList
  • public virtual bool ContainsValue( object value);   Determine whether SortedList contains a specific value
  • public virtual object GetByIndex( int index );   Get the value at the specified index in SortedList
  • public virtual object GetKey( int index );   Get the key at the specified index in SortedList
  • public virtual IList GetKeyList();   Get the key of SortedList
  • public virtual IList GetValueList();   Get the value in SortedList
  • public virtual int IndexOfKey( object key);   Returns the index from zero in the sorted list
  • public virtual int IndexOfValue( object value);   Returns the index starting from zero when the value specified in SortedList first appears
  • public virtual void Remove( object key);   Delete elements of the specified key from the SortedList table
  • public virtual void RemoveAt( int index );   Delete the element at the specified index in SortedList
  • public virtual void TrimToSize();   Set the actual number of elements in SortedList

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links