System.Collections.Generic.List

Added in .NET 2.0
Represents a collection of objects that can be individually accessed by an index
Appending elements is fast but inserting elements at a given position is slow
Searching using binary search is fast if the list has already been sorted.
This type of collection represents a collection on keys and values
provides access by index numbers


System.Collections.Generic.List 

Fixed Size

Allows modification of elements but does not allow the addition or removal of elements.

myList = new System.Collections.Generic.List<string>(3); 
myList.Add("one");
myList.Add("two");
myList.Add("three");
System.Diagnostics.Debug.Print System.String.Format("Count={0}", myList.Count)

System.Diagnostics.Debug.Print myList[2];





Variable Size

Allows addition, removal and modification of elements.



Where



GenericList.Add = how to check of its already been added ? 


Distinct


GenericList.Distinct.ToList 


ToArray

copy the list to an array


get a value at a given index
get a subrange/sublist between 2 index positions


reverse the order of the items


get an enumerator for the list


Declaring


Items - Add / Remove



Add


System.Collections.Generic.List<string> myList; 
myList = new System.Collections.Generic.List<string>(3);

myList.Add("one");
myList.Add("two");
myList.Add("three");
myList.Add "four";

myList.Add(new object[] {"text",50,False)};

AddRange

add multiple elements

System.Collections.Generic.List<string> myList; 
myList = new System.Collections.Generic.List<string>(3);
string sConCat;

sConCat = "one;two;three"
myList.AddRange(sConCat.Split(Ctype(";", Char)))

Insert a single element


Insert multiple elements




InsertRange

System.Collections.Generic.List<string> moreItems; 
moreItems = new System.Collections.Generic.List<string>(4);

for (int i=0; i<moreItems.Count; i++)
{
   moreItems[i] = 200;
}
myList.InsertRange(2, moreItems);


Removing


remove a single element

myList.Remove("two"); 

remove all elements based on a matching predicate



remove all

myList.Clear(); 

Items - Updating


Items - Finding


Exists

System.Collections.Generic.List<int> mylist; 
mylist = new System.Collections.Generic.List<int>();
mylist.Add(10);
mylist.Add(20);
if (mylist.Exists(x => x == 20) == false)
{
}

First

If you try and use First when there are no values, an exception will be thrown


FirstOrDefault

If you try and use FirstOrDefault when there are no values, the default value will be returned instead of an exception



How to quickly find a particular item anything equivalent to a Dictionary - ContainsKey ?


Looping


foreach (string myText in myList) 
{
   System.Console.WriteLine(myText);
}

Sorting


Sort (System.Comparison<T> comparison)

Sorts the elements in a list by making use of the System.Comparison<T> delegate

public delegate int Comparison <in T>(T x, T y) 

This delegate must return a signed integer that indicates if
-1 - if x is less than y
1 - if y is less than x
0 - if x equals y


System.Collections.Generic.List<string> mylist; 
mylist = new System.Collections.Generic.List<string>();
mylist.Add("hhh");
mylist.Add("bbb");
mylist.Add("");
mylist.Add(null);
mylist.Add("aaa");
mylist.Add("kkk");
mylist.Sort(CompareMyItems);

private static int CompareMyItems(string x, string y)
{
   if (x==null)
   {
      if (y==null) { return 0; } //both null
      else { return 1; } //y > null
   }
   else
   {
      if (y==null) { return 1; } //x > null
      else
      {
         int retval = x.Length.CompareTo(y.Length);
         if (retval==0) { return x.CompareTo(y);
         else { return retval; }
      }
   }
}


System.Collections.Generic.IList<T>

Added in .NET 2.0
The standard interface for indexable collections
It supports all the methods of ICollection<T> and IEnumerable<T>
get the value of set the value at any position (index value)
find the index of a particular value
Insert at a particular index position
Remove an item at a particular index position


public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable 

This is the common interface for indexable collections
This contains all the methods of ICollection<T> and IEnumerable<T>
You can get the value (or set the value) at any given position
You can find the index of a specific item
You can insert an item at any position
You can remove an item from any position



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext