For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects. This sheet focuses on collections part of it and will try to give you pointer when to use which
|
Class
|
Description
|
|
ArrayList
|
Represents an array of objects whose size is dynamically increased as required.
|
|
Hashtable
|
Represents a collection of key/value pairs that are organized based on the hash code of the key.
|
|
Queue
|
Represents a first in, first out (FIFO) collection of objects.
|
|
Stack
|
Represents a last in, first out (LIFO) collection of objects.
|
// Create a list of parts.List<Part> parts = new List<Part>();// Add parts to the list.parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });parts.Add(new Part() { PartName = "cassette", PartId = 1534 });parts.Add(new Part() { PartName = "shift lever", PartId = 1634 });|
Method
|
Usage
|
Example
|
|
List<T>.Add(T)
|
Adds an object to the end of the List<T>.
|
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 }); |
|
List<T>.Remove(T)
|
Removes the first occurrence of a specific object from the List<T>.
|
parts.Remove(new Part() { PartId = 1534, PartName = "cogs" }); |
|
List<T>.Clear
|
Removes all elements from the List<T>.
|
parts.Clear(); |
|
List<T>.Contains(T)
|
Determines whether an element is in the List<T>.
|
parts.Contains(new Part { PartId = 1734, PartName = "" })); |
|
List<T>.Sort
|
Sorts the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.
|
parts.Sort(); |
// Create a stack of stringsStack<string> numbers = new Stack<string>();// Add items to the stacknumbers.Push("one");numbers.Push("two");|
Method
|
Usage
|
Example
|
|
Stack<T>.Push(T)
|
Inserts an object at the top of the Stack<T>.
|
numbers.Push("one"); |
|
Stack<T>.Pop
|
Removes and returns the object at the top of the Stack<T>.
|
numbers.Pop(); |
|
Stack<T>.Push(T)
|
Represents a first in, first out (FIFO) collection of objects.
|
numbers.Push("one"); |
|
Stack<T>.Peek
|
The object at the top of the Stack<T>.
|
numbers.Peek(); |
|
Stack<T>.Contains(T)
|
Determines whether an element is in the Stack<T>.
|
stack2.Contains("four"); |
|
Stack<T>.Clear
|
Removes all objects from the Stack<T>.
|
stack2.Clear(); |
HashSet<int> evenNumbers = new HashSet<int>();HashSet<int> oddNumbers = new HashSet<int>(); for (int i = 0; i < 5; i++){ // Populate numbers with just even numbers. evenNumbers.Add(i 2); // Populate oddNumbers with just odd numbers. oddNumbers.Add((i 2) + 1);}|
Method
|
Usage
|
Example
|
|
HashSet<T>.Add(T)
|
Adds the specified element to a set.
|
evenNumbers.Add(i * 2); |
|
HashSet<T>.Remove(T)
|
Removes all elements from a HashSet<T> object.
|
numbers.Remove(0); |
|
HashSet<T>.Clear
|
Represents a first in, first out (FIFO) collection of objects.
|
numbers.Clear(); |
|
HashSet<T>.Contains(T)
|
Determines whether a HashSet<T> object contains the specified element.
|
numbers.Contains(0) |
|
Class
|
Description
|
|
Dictionary<TKey,TValue>
|
Represents a collection of key/value pairs that are organized based on the key.
|
|
List<T>
|
Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.
|
|
Queue<T>
|
Represents a first in, first out (FIFO) collection of objects.
|
|
SortedList<TKey,TValue>
|
Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation.
|
|
Stack<T>
|
Represents a last in, first out (LIFO) collection of objects.
|
Create a queue of stringsQueue<string> numbers = new Queue<string>();Add items in the queuenumbers.Enqueue("one");numbers.Enqueue("two");numbers.Enqueue("three");|
Method
|
Usage
|
Example
|
|
Queue<T>.Enqueue(T)
|
Adds an object to the end of the Queue<T>.
|
numbers.Enqueue("one"); |
|
Queue<T>.Dequeue
|
Removes and returns the object at the beginning of the Queue<T>.
|
numbers.Dequeue(); |
|
Queue<T>.Peek
|
The object at the beginning of the Queue<T>.
|
numbers.Peek(); |
|
Queue<T>.Contains(T)
|
Determines whether an element is in the Queue<T>.
|
numbers.Contains(0) |
// Create a new dictionary of strings, with string keys.Dictionary<string, string> openWith = new Dictionary<string, string>();openWith.Add("txt", "notepad.exe");|
Method
|
Usage
|
Example
|
|
Dictionary<TKey,TValue>.Add(TKey, TValue)
|
Adds the specified key and value to the dictionary.
|
openWith.Add("txt", "notepad.exe"); |
|
Dictionary<TKey,TValue>.Remove
|
Removes the value with the specified key from the Dictionary<TKey,TValue>.
|
public bool Remove (TKey key); |
|
Dictionary<TKey,TValue>.Clear
|
Removes all keys and values from the Dictionary<TKey,TValue>.
|
public void Clear (); |
|
Dictionary<TKey,TValue>.ContainsKey(TKey)
|
Determines whether the Dictionary<TKey,TValue> contains the specified key.
|
public bool ContainsKey (TKey key); |
|
Dictionary<TKey,TValue>.ContainsValue(TValue)
|
Determines whether the Dictionary<TKey,TValue> contains a specific value.
|
public bool ContainsValue (TValue value); |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.