C# Collection Cheat Sheet
by masterofcode via cheatography.com/140841/cs/30006/
System.Collections Classes List<T> Methods (cont)
Class Description List<T Sorts the elements or a portion of the parts.Sort()
ArrayList Represents an array of objects whose size is dynami‐ >.Sort elements in the List<T> using either ;
cally increased as required. the specified or default IComparer<T>
implementation or a provided Compar‐
Hashtable Represents a collection of key/value pairs that are
ison<T> delegate to compare list
organized based on the hash code of the key.
elements.
Queue Represents a first in, first out (FIFO) collection of
For further information and examples visit this link
objects.
Stack Represents a last in, first out (LIFO) collection of
Stack<T> Class
objects.
Namespace: System.Collections.Generic
List<T> Class Assembly: System.Collections.dll
Namespace: System.Collections.Generic
Specifies the type of elements in the stack.
Assembly: System.Collections.dll
// Create a stack of strings
Stack<string> numbers = new Stack<string>();
Represents a strongly typed list of objects that can be accessed by index. Provides methods
// Add items to the stack
to search, sort, and manipulate lists.
numbers.Push("one");
// Create a list of parts.
numbers.Push("two");
List<Part> parts = new List<Part>();
// Add parts to the list.
Stack<T> Methods
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring", PartIdMethod Usage
= 1334 }); Example
Stack<
parts.Add(new Part() { PartName = "regular seat", PartId = ‐1434Inserts
}) an object at numbers.Push("one");
; T>.P‐ the top of the
ush(T)
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 Stack<
}); T>.
parts.Add(new Part() { PartName = "cassette", PartIdStack<
= 1534‐ Removes and
}); numbers.Pop();
T>.Pop
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 returns
}); the object at
the top of the
List<T> Methods Stack<T>.
Stack<‐ Represents a first in, numbers.Push("one");
T>.P‐ first out (FIFO)
ush(T) collection of objects.
Stack<‐ The object at the top numbers.Peek();
T>.Peek of the Stack<T>.
Stack<‐ Determines whether stack2.Contains("fou
T>.Con‐ an element is in the r");
tains(T) Stack<T>.
Stack<‐ Removes all objects stack2.Clear();
T>.Clear from the Stack<T>.
For further information and examples visit this link
Method Usage Example
List<T‐ Adds an parts.Add(new Part() { PartName = "crank arm", PartId = 1234 })
>.A‐ object to ;
dd(T) the end of
the List<T‐
>.
List<T‐ Removes parts.Remove(new Part() { PartId = 1534, PartName = "cogs" })
>.R‐ the first ;
emo‐ occurrence
ve(T) of a
specific
object from
the List<T‐
>.
List<T‐ Removes parts.Clear();
>.Clear all
elements
from the
List<T>.
List<T‐ Determines parts.Contains(new Part { PartId = 1734, PartName = "" }));
>.Cont‐ whether an
ains(T) element is
in the
List<T>.
By masterofcode Published 2nd December, 2021. Sponsored by Readable.com
Last updated 3rd December, 2021. Measure your website readability!
Page 1 of 5. https://readable.com
cheatography.com/masterofcode/
C# Collection Cheat Sheet
by masterofcode via cheatography.com/140841/cs/30006/
HashSet<T> Class Queue<T> Class
Namespace: System.Collections.Generic Namespace: System.Collections.Generic
Assembly: System.Collections.dll Assembly: System.Collections.dll
Represents a set of values. Represents a first-in, first-out collection of objects.
HashSet<int> evenNumbers = new HashSet<int>(); Create a queue of strings
HashSet<int> oddNumbers = new HashSet<int>(); Queue<string> numbers = new Queue<string>();
Add items in the queue
for (int i = 0; i < 5; i++) numbers.Enqueue("one");
{ numbers.Enqueue("two");
// Populate numbers with just even numbers. numbers.Enqueue("three");
evenNumbers.Add(i * 2);
Queue<T> Methods
// Populate oddNumbers with just odd numbers. Method Usage Example
oddNumbers.Add((i * 2) + 1);
Queue<‐ Adds an object to the numbers.Enqueue("on
}
T>.Enq‐ end of the Queue<T>. e");
ueue(T)
HashSet<T> Methods
Queue<‐ Removes and returns numbers.Dequeue();
Method Usage Example T>.D‐ the object at the
HashSe‐ Adds the specified evenNumbers.Add(i * equeue beginning of the
t<T>.A‐ element to a set. 2); Queue<T>.
dd(T) Queue<‐ The object at the numbers.Peek();
HashSe‐ Removes all elements numbers.Remove(0); T>.Peek beginning of the
t<T>.R‐ from a HashSet<T> Queue<T>.
emo‐ object. Queue<‐ Determines whether numbers.Contains(0)
ve(T) T>.Con‐ an element is in the
HashSe‐ Represents a first in, numbers.Clear(); tains(T) Queue<T>.
t<T‐ first out (FIFO) For further information and examples visit this link
>.Clear collection of objects.
HashSe‐ Determines whether a numbers.Contains(0) Dictionary<TKey,TValue> Class
t<T>.C‐ HashSet<T> object
Namespace: System.Collections.Generic
ont‐ contains the specified
Assembly: System.Collections.dll
ains(T) element.
For further information and examples visit this link Represents a collection of keys and values.
// Create a new dictionary of strings, with string keys.
System.Collections.Generic Classes Dictionary<string, string> openWith =
new Dictionary<string, string>();
openWith.Add("txt", "notepad.exe");
Dictionary<TKey,TValue> Methods
Class Description Method Usage Example
Dictionar‐ Represents a collection of key/value pairs that are Dictio‐ Adds the openWith.Add("txt", "notepad
y<T‐ organized based on the key. nar‐ specified .exe");
Key,TV‐ y<T‐ key and
alue> Key,TV‐ value to
List<T> Represents a list of objects that can be accessed by alu‐ the
index. Provides methods to search, sort, and modify e>.A‐ dictio‐
lists. dd‐ nary.
(TKey,
Queue<T> Represents a first in, first out (FIFO) collection of
TValue)
objects.
Dictio‐ Removes public bool Remove (TKey key);
SortedLis‐ Represents a collection of key/value pairs that are
nar‐ the value openWith.Remove("doc");
t<T‐ sorted by key based on the associated IComparer<T>
y<T‐ with the
Key,TV‐ implementation.
Key,TV‐ specified
alue>
alu‐ key from
Stack<T> Represents a last in, first out (LIFO) collection of
e>.R‐ the
objects.
emove Dictio‐
nary<T‐
Key,TV‐
alue>.
By masterofcode Published 2nd December, 2021. Sponsored by Readable.com
Last updated 3rd December, 2021. Measure your website readability!
Page 2 of 5. https://readable.com
cheatography.com/masterofcode/
C# Collection Cheat Sheet
by masterofcode via cheatography.com/140841/cs/30006/
Dictionary<TKey,TValue> Methods (cont)
Dictio‐ Removes public void Clear ();
nar‐ all keys openWith.Clear();
y<T‐ and values
Key,T‐ from the
Valu‐ Dictionar‐
e>.C‐ y<T‐
lear Key,TValu‐
e>.
Dictio‐ Determines public bool ContainsKey (TKey key);
nar‐ whether openWith.ContainsKey("ht");
y<T‐ the Dictio‐
Key,T‐ nary<T‐
Valu‐ Key,TValu‐
e>.C‐ e>
ontai‐ contains
nsK‐ the
ey(‐ specified
TKey) key.
Dictio‐ Determines public bool ContainsValue (TValue value);
nar‐ whether
y<T‐ the Dictio‐ openWith.ContainsValue("hypertrm.
Key,T‐ nary<T‐ exe");
Valu‐ Key,TValu‐
e>.C‐ e>
ontai‐ contains a
nsV‐ specific
alu‐ value.
e(T‐
Value)
For further information and examples visit this link
By masterofcode Published 2nd December, 2021. Sponsored by Readable.com
Last updated 3rd December, 2021. Measure your website readability!
Page 3 of 5. https://readable.com
cheatography.com/masterofcode/