Skip to content

codewithelmor/csharp-ilist-list-comparison

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

IList List Comparison

In C#, IList and List are related but distinct concepts.

  1. IList Interface:
  • IList stands for "interface list."
  • It is part of the System.Collections namespace.
  • IList is an interface, which means it defines a contract for a collection of objects that can be enumerated and accessed by index.
  • It provides methods for adding, removing, and retrieving elements from a collection.
  • As an interface, it does not provide any implementation. Classes that implement IList must provide their own implementation for its members.
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
    // Methods for adding, removing, and accessing elements
}
  1. List Class:
  • List is a concrete class that implements the IList interface.
  • It is part of the System.Collections.Generic namespace.
  • List is a dynamic array that automatically grows and shrinks as needed.
  • It provides an array-based implementation of the IList interface.
  • It is one of the most commonly used collection types in C#.
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyList<T>, IReadOnlyCollection<T>, IList, ICollection, IEnumerable
{
    // Implementation of IList interface
}

When you use a List, you get all the functionality defined in the IList interface, and you also benefit from the specific implementation provided by the List class.

Example:

// Using IList
IList<string> stringList = new List<string>();
stringList.Add("Item 1");
stringList.Add("Item 2");

// Using List
List<int> intList = new List<int>();
intList.Add(1);
intList.Add(2);

In general, if you only need the features provided by the IList interface and want to be more flexible with the underlying implementation, you might choose to work with IList. However, if you need a dynamic array-based implementation with additional features, you would use the List class.

C# List Additional Features

The List<T> class in C# provides additional features beyond what is included in the IList<T> interface. Some of the notable features of List<T> include:

  1. Capacity Management:
  • Capacity: The List<T> class has a Capacity property that allows you to get or set the number of elements that the internal array can hold without resizing. This can be useful for optimizing performance when you know the expected size of the list in advance.

  • TrimExcess(): This method sets the capacity to the actual number of elements in the list, if it is less than a threshold. This can be used to minimize the memory overhead when the list is not expected to grow further.

  1. Range Operations:
  • GetRange(): The List<T> class provides a GetRange(int index, int count) method that returns a new List<T> containing elements from a specified range of the original list.

  • RemoveRange(): Allows removal of a specified range of elements from the list.

  • InsertRange(): Enables insertion of a collection of elements at a specified index in the list.

  1. Binary Search:
  • BinarySearch(): The List<T> class provides a binary search method for efficiently searching for an element in a sorted list.
  1. Sorting:
  • Sort(): The List<T> class includes a Sort() method to sort the elements in the list. You can also provide a custom IComparer<T> to define a custom sorting order.
  1. Reversing:
  • Reverse(): Reverses the order of elements in the entire list.
  1. ForEach Method:
  • ForEach(): The List<T> class has a ForEach method that allows you to perform a specified action on each element of the list.
  1. Capacity Expansion:
  • The List<T> class dynamically adjusts its capacity as elements are added, avoiding the need to manually resize the underlying array.
  1. AddRange Method:
  • AddRange(): Allows you to add a collection of elements to the end of the list.
  1. IndexOf/LastIndexOf Overloads:
  • IndexOf() and LastIndexOf() methods have overloads that allow you to specify the index from which the search should begin.

These additional features make List<T> a powerful and flexible collection class, especially when you need to work with dynamic lists of elements. Keep in mind that, while List<T> provides these features, it also comes with some trade-offs in terms of memory usage, as it may allocate more space than is currently needed to accommodate future growth.

About

C# IList vs List Comparison

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published