Collection

Data structures are an essential part of programming that help organize and manipulate data efficiently. In C#, the System.Collections and System.Collections.Generic namespaces provide a wide range of data structures to choose from. In this list, we will explore all the data structures present in these namespaces. These include ArrayList, Dictionary, HashSet, LinkedList, Queue, SortedDictionary, SortedList, SortedSet, and many more. Each of these data structures has unique characteristics and can be used for different purposes depending on the requirements of your project. By understanding these data structures, you can write more efficient and optimized code that can handle large amounts of data.

Here’s a list of all the data structures in the System.Collections and System.Collections.Generic namespaces in C#:

CollectionsCollections.Generic
ArrayListDictionary<TKey, TValue>
BitArrayHashSet<T>
CaseInsensitiveComparerIComparer<T>
CaseInsensitiveHashCodeProviderIDictionary<TKey, TValue>
CollectionBaseIEnumerable<T>
ComparerIEnumerator<T>
DictionaryBaseIList<T>
HashtableISet<T>
ICollectionKeyNotFoundException
IComparerKeyValuePair<TKey, TValue>
IDictionaryLinkedList<T>
IEnumerableList<T>
IEnumeratorQueue<T>
IEqualityComparerSortedDictionary<TKey, TValue>
IListSortedList<TKey, TValue>
IStructuralComparableSortedSet<T>
IStructuralEquatableStack<T>
QueueICollection<T>
ReadOnlyCollectionBaseIReadOnlyCollection<T>
SortedListIReadOnlyDictionary<TKey, TValue>
Stack-

The data structures that are listed in the System.Collections and System Collections.Generic namespaces in  C#, are used to store a collection of objects. However, there are some significant differences in the implementation, functionality, and performance of these data structures. Here are some of the key distinctions:

In summary, the data structures in the System.Collections.Generic namespaces are generally faster, more memory efficient, and more type-safe than the ones in the System.Collections namespace and they offer more functionality and flexibility. However, both sets of data structures have their use cases and can be useful in different situations.


Now, here’s a list of some of the most commonly used data structures from the System.Collections and System.Collections.Generic namespaces in C#:

CollectionCollection.Generic
ArrayListList<T>
HashtableDictionary<TKey, TValue>
-HashSet<T>
QueueQueue<T>
StackStack<T>

But when to use each of these data structures:

And it depends on your application.


Here are some of the most used List<T> methods:

// List<T>.Add
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// numbers now contains {1, 2, 3}
// List<T>.Clear
numbers.Clear();
// numbers is now an empty list
// List<T>.Contains
List<string> fruits = new List<string>() { "apple", "banana", "orange" };
bool containsBanana = fruits.Contains("banana");
// containsBanana is true
// List<T>.CopyTo
int[] array = new int[5];
List<int> list = new List<int>() { 1, 2, 3 };
list.CopyTo(array, 1);
// array now contains {0, 1, 2, 3, 0}
// List<T>.Count
int count = list.Count;
// count is 3
// List<T>.IndexOf
int index = fruits.IndexOf("orange");
// index is 2
// List<T>.Insert
fruits.Insert(1, "grape");
// fruits now contains {"apple", "grape", "banana", "orange"}
// List<T>.InsertRange
List<int> newNumbers = new List<int>() { 4, 5 };
numbers.InsertRange(1, newNumbers);
// numbers now contains {1, 4, 5, 2, 3}
// List<T>.Remove
numbers.Remove(5);
// numbers now contains {1, 4, 2, 3}
// List<T>.RemoveAll
numbers.RemoveAll(n => n % 2 == 0);
// numbers now contains {1}
// List<T>.RemoveAt
numbers.RemoveAt(0);
// numbers is now an empty list
// List<T>.RemoveRange
numbers.AddRange(new List<int>() { 1, 2, 3, 4, 5 });
numbers.RemoveRange(1, 3);
// numbers now contains {1, 5}
// List<T>.Reverse
numbers.Reverse();
// numbers now contains {5, 1}
// List<T>.Sort
numbers.Sort();
// numbers now contains {1, 5}
// List<T>.ToArray
int[] numberArray = numbers.ToArray();
// numberArray now contains {1, 5}

Here are some of the most used Dictionary<TKey, TValue> methods:

// Dictionary<TKey, TValue>.Add
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
myDictionary.Add("apple", 1);
myDictionary.Add("banana", 2);
myDictionary.Add("orange", 3);
// Dictionary<TKey, TValue>.Clear
myDictionary.Clear();
// Dictionary<TKey, TValue>.ContainsKey
myDictionary.Add("apple", 1);
bool containsApple = myDictionary.ContainsKey("apple");
// containsApple is true
// Dictionary<TKey, TValue>.ContainsValue
bool containsValueThree = myDictionary.ContainsValue(3);
// containsValueThree is false
// Dictionary<TKey, TValue>.Count
int count = myDictionary.Count;
// count is 1
// Dictionary<TKey, TValue>.GetEnumerator
Dictionary<string, int>.Enumerator enumerator = myDictionary.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine("Key: {0}, Value: {1}", enumerator.Current.Key, enumerator.Current.Value);
}
// Dictionary<TKey, TValue>.Remove
myDictionary.Remove("apple");
// Dictionary<TKey, TValue>.TryGetValue
int value;
if (myDictionary.TryGetValue("banana", out value))
{
Console.WriteLine("The value of 'banana' is {0}", value);
}
else
{
Console.WriteLine("'banana' is not in the dictionary.");
}
// Dictionary<TKey, TValue>.Keys
ICollection<string> keys = myDictionary.Keys;
foreach (string key in keys)
{
Console.WriteLine(key);
}
// Dictionary<TKey, TValue>.Values
ICollection<int> values = myDictionary.Values;
foreach (int value in values)
{
Console.WriteLine(value);
}

Here are some of the most used HashSet<T> methods :

// HashSet<T>.Add
HashSet<string> colors = new HashSet<string>();
colors.Add("red");
colors.Add("green");
colors.Add("blue");
// colors now contains {"red", "green", "blue"}
// HashSet<T>.Clear
colors.Clear();
// colors is now an empty set
// HashSet<T>.Contains
HashSet<int> numbers = new HashSet<int>() { 1, 2, 3 };
bool containsTwo = numbers.Contains(2);
// containsTwo is true
// HashSet<T>.Count
int count = numbers.Count;
// count is 3
// HashSet<T>.ExceptWith
HashSet<int> otherNumbers = new HashSet<int>() { 2, 4 };
numbers.ExceptWith(otherNumbers);
// numbers now contains {1, 3}
// HashSet<T>.IntersectWith
numbers.IntersectWith(otherNumbers);
// numbers now contains {2}
// HashSet<T>.Remove
colors.Remove("green");
// colors now contains {"red", "blue"}
// HashSet<T>.UnionWith
setA.UnionWith(setB);
// setA now contains {1, 2, 3, 4, 5}

Here are some of the most used Queue<T> methods;

// Queue<T>.Clear
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
queue.Clear();
// queue is now empty
// Queue<T>.Enqueue
queue.Enqueue(4);
// queue now contains {2, 3, 4}
// Queue<T>.Dequeue
int firstNum = queue.Dequeue();
// firstNum is 1, and queue now contains {2, 3}
// Queue<T>.Contains
Queue<string> names = new Queue<string>();
names.Enqueue("Alice");
names.Enqueue("Bob");
names.Enqueue("Charlie");

bool containsBob = names.Contains("Bob");
// containsBob is true
// Queue<T>.CopyTo
Queue<double> nums = new Queue<double>();
nums.Enqueue(1.1);
nums.Enqueue(2.2);
nums.Enqueue(3.3);
double[] arr = new double[3];
nums.CopyTo(arr, 0);
// arr now contains {1.1, 2.2, 3.3}
// Queue<T>.GetEnumerator
IEnumerator<int> enumerator = queue.GetEnumerator();
while (enumerator.MoveNext())
{
int num = enumerator.Current;
// do something with num
}
// Queue<T>.Peek
int firstNum = queue.Peek();
// firstNum is 2, and queue still contains {2, 3, 4}
// Queue<T>.ToArray
int[] numsArr = queue.ToArray();
// numsArr contains {2, 3, 4}

Here are some of the most used Stack<T> methods;

// Stack<T>.Clear
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Clear();
// stack is now empty
// Stack<T>.Contains
Stack<string> stackNames = new Stack<string>();
stackNames.Push("Alice");
stackNames.Push("Bob");
stackNames.Push("Charlie");
bool stackContainsBob = stackNames.Contains("Bob");
// containsBob is true
// Stack<T>.CopyTo
Stack<double> stackNumbers = new Stack<double>();
stackNumbers.Push(1.1);
stackNumbers.Push(2.2);
stackNumbers.Push(3.3);
double[] arr2 = new double[3];
nums.CopyTo(arr2, 0);
// arr now contains {3.3, 2.2, 1.1} (the elements are reversed)
// Stack<T>.GetEnumerator
IEnumerator<double> enumerator = stackNumbers.GetEnumerator();
while (enumerator.MoveNext())
{
double num = enumerator.Current;
// do something with num
}
// Stack<T>.Peek
double topNum = stackNumbers.Peek();
// topNum is 3.3, and stackNumbers still contains {1.1, 2.2, 3.3}
// Stack<T>.Pop
double poppedNum = stackNumbers.Pop();
// poppedNum is 3.3, and stackNumbers now contains {1.1, 2.2}
// Stack<T>.Push
stackNumbers.Push(4.4);
// stackNumbers now contains {4.4, 1.1, 2.2}
// Stack<T>.ToArray
double[] numsArr = stackNumbers.ToArray();
// numsArr contains {4.4, 1.1, 2.2}

Return to Index