Show Menu
Cheatography

C# Collection Cheat Sheet by

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

System.Co­lle­ctions Classes

Class
Descri­ption
ArrayList
Represents an array of objects whose size is dynami­cally 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.

List<T> Class

Namespace: System.Co­lle­cti­ons.Ge­neric
Assembly: System.Co­lle­cti­ons.dll

Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

// Create a list of parts.

List<P­art> parts = new List<P­art­>();

// Add parts to the list.

parts.A­dd(new Part() { PartName = "­crank arm", PartId = 1234 });

parts.A­dd(new Part() { PartName = "­chain ring", PartId = 1334 });

parts.A­dd(new Part() { PartName = "­regular seat", PartId = 1434 });

parts.A­dd(new Part() { PartName = "­banana seat", PartId = 1444 });

parts.A­dd(new Part() { PartName = "­cas­set­te", PartId = 1534 });

parts.A­dd(new Part() { PartName = "­shift lever", PartId = 1634 });

List<T> Methods

Method
Usage
Example
List<T­>.A­dd(T)
Adds an object to the end of the List<T­>.
parts.A­dd(new Part() { PartName = "­crank arm", PartId = 1234 });
List<T­>.R­emo­ve(T)
Removes the first occurrence of a specific object from the List<T­>.
parts.R­em­ove(new Part() { PartId = 1534, PartName = "­cog­s" });
List<T­>.Clear
Removes all elements from the List<T­>.
parts.C­le­ar();
List<T­>.C­ont­ains(T)
Determines whether an element is in the List<T­>.
parts.C­on­tai­ns(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 ICompa­rer­<T> implem­ent­ation or a provided Compar­iso­n<T> delegate to compare list elements.
parts.S­ort();
For further inform­ation and examples visit this link

Stack<­T> Class

Namespace: System.Co­lle­cti­ons.Ge­neric
Assembly: System.Co­lle­cti­ons.dll

Specifies the type of elements in the stack.


// Create a stack of strings

Stack<­str­ing> numbers = new Stack<­str­ing­>();

// Add items to the stack

number­s.P­ush­("on­e");

number­s.P­ush­("tw­o");

Stack<­T> Methods

Method
Usage
Example
Stack<­T>.P­ush(T)
Inserts an object at the top of the Stack<­T>.
number­s.P­ush­("on­e");
Stack<­T>.Pop
Removes and returns the object at the top of the Stack<­T>.
number­s.P­op();
Stack<­T>.P­ush(T)
Represents a first in, first out (FIFO) collection of objects.
number­s.P­ush­("on­e");
Stack<­T>.Peek
The object at the top of the Stack<­T>.
number­s.P­eek();
Stack<­T>.C­on­tai­ns(T)
Determines whether an element is in the Stack<­T>.
stack2.Co­nta­ins­("fo­ur");
Stack<­T>.C­lear
Removes all objects from the Stack<­T>.
stack2.Cl­ear();
For further inform­ation and examples visit this link

HashSe­t<T> Class

Namespace: System.Co­lle­cti­ons.Ge­neric
Assembly: System.Co­lle­cti­ons.dll

Represents a set of values.

HashSe­t<i­nt> evenNu­mbers = new HashSe­t<i­nt>();

HashSe­t<i­nt> oddNumbers = new HashSe­t<i­nt>();

 

for (int i = 0; i < 5; i++)

{

    // Populate numbers with just even numbers.

    evenNu­mbe­rs.A­dd(i * 2);

 

    // Populate oddNumbers with just odd numbers.

    oddNum­ber­s.A­dd((i * 2) + 1);

}

HashSe­t<T> Methods

Method
Usage
Example
HashSe­t<T­>.A­dd(T)
Adds the specified element to a set.
evenNu­mbe­rs.A­dd(i * 2);
HashSe­t<T­>.R­emo­ve(T)
Removes all elements from a HashSe­t<T> object.
number­s.R­emo­ve(0);
HashSe­t<T­>.Clear
Represents a first in, first out (FIFO) collection of objects.
number­s.C­lear();
HashSe­t<T­>.C­ont­ains(T)
Determines whether a HashSe­t<T> object contains the specified element.
number­s.C­ont­ains(0)
For further inform­ation and examples visit this link
 

System.Co­lle­cti­ons.Ge­neric Classes

Class
Descri­ption
Dictio­nar­y<T­Key­,TV­alu­e>
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.
Sorted­Lis­t<T­Key­,TV­alu­e>
Represents a collection of key/value pairs that are sorted by key based on the associated ICompa­rer­<T> implem­ent­ation.
Stack<­T>
Represents a last in, first out (LIFO) collection of objects.

Queue<­T> Class

Namespace: System.Co­lle­cti­ons.Ge­neric
Assembly: System.Co­lle­cti­ons.dll

Represents a first-in, first-out collection of objects.

Create a queue of strings

Queue<­str­ing> numbers = new Queue<­str­ing­>();

Add items in the queue

number­s.E­nqu­eue­("on­e");

number­s.E­nqu­eue­("tw­o");

number­s.E­nqu­eue­("th­ree­");

Queue<­T> Methods

Method
Usage
Example
Queue<­T>.E­nq­ueue(T)
Adds an object to the end of the Queue<­T>.
number­s.E­nqu­eue­("on­e");
Queue<­T>.D­equeue
Removes and returns the object at the beginning of the Queue<­T>.
number­s.D­equ­eue();
Queue<­T>.Peek
The object at the beginning of the Queue<­T>.
number­s.P­eek();
Queue<­T>.C­on­tai­ns(T)
Determines whether an element is in the Queue<­T>.
number­s.C­ont­ains(0)
For further inform­ation and examples visit this link

Dictio­nar­y<T­Key­,TV­alu­e> Class

Namespace: System.Co­lle­cti­ons.Ge­neric
Assembly: System.Co­lle­cti­ons.dll

Represents a collection of keys and values.

// Create a new dictionary of strings, with string keys.

Dictio­nar­y<s­tring, string> openWith =

   new Dictio­nar­y<s­tring, string­>();

openWi­th.A­dd­("tx­t", "­not­epa­d.e­xe");

Dictio­nar­y<T­Key­,TV­alu­e> Methods

Method
Usage
Example
Dictio­nar­y<T­Key­,TV­alu­e>.A­dd­(TKey, TValue)
Adds the specified key and value to the dictio­nary.
openWi­th.A­dd­("tx­t", "­not­epa­d.e­xe");
Dictio­nar­y<T­Key­,TV­alu­e>.R­emove
Removes the value with the specified key from the Dictio­nar­y<T­Key­,TV­alu­e>.
public bool Remove (TKey key); 
openWi­th.R­em­ove­("do­c");
Dictio­nar­y<T­Key­,TV­alu­e>.C­lear
Removes all keys and values from the Dictio­nar­y<T­Key­,TV­alu­e>.
public void Clear (); 
openWi­­th.Cl­ear();
Dictio­nar­y<T­Key­,TV­alu­e>.C­on­tai­nsK­ey(­TKey)
Determines whether the Dictio­nar­y<T­Key­,TV­alu­e> contains the specified key.
public bool Contai­nsKey (TKey key); 
openWi­th.C­on­tai­nsK­ey(­"­ht");
Dictio­nar­y<T­Key­,TV­alu­e>.C­on­tai­nsV­alu­e(T­Value)
Determines whether the Dictio­nar­y<T­Key­,TV­alu­e> contains a specific value.
public bool Contai­nsValue (TValue value);  
openWi­th.C­on­tai­nsV­alu­e("h­ype­rtr­m.e­xe");
For further inform­ation and examples visit this link
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          C# Naming Conventions Cheat Sheet
          C# CheatSheet Cheat Sheet