0% found this document useful (0 votes)
11 views

Masterofcode C-Collection

The document provides a cheat sheet on common C# collection classes like List, Stack, Queue, HashSet and Dictionary. It includes the namespace, description and examples of common methods for each collection class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Masterofcode C-Collection

The document provides a cheat sheet on common C# collection classes like List, Stack, Queue, HashSet and Dictionary. It includes the namespace, description and examples of common methods for each collection class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Collection Cheat Sheet

by masterofcode via cheatography.com/140841/cs/30006/

System.Co​lle​ctions Classes List<T> Methods (cont)

Class Descri​ption List<T​ Sorts the elements or a portion of the parts.S​ort()


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 ICompa​rer​<T>
implem​ent​ation or a provided Compar​‐
Hashtable Represents a collection of key/value pairs that are
iso​n<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 inform​ation and examples visit this link
objects.
Stack Represents a last in, first out (LIFO) collection of
Stack<​T> Class
objects.
Namespace: System.Co​lle​cti​ons.Ge​neric
List<T> Class Assembly: System.Co​lle​cti​ons.dll

Namespace: System.Co​lle​cti​ons.Ge​neric
Specifies the type of elements in the stack.
Assembly: System.Co​lle​cti​ons.dll
// Create a stack of strings
Stack<​str​ing> numbers = new Stack<​str​ing​>();
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.
number​s.P​ush​("on​e");
// Create a list of parts.
number​s.P​ush​("tw​o");
List<P​art> parts = new List<P​art​>();
// Add parts to the list.
Stack<​T> Methods
parts.A​dd(new Part() { PartName = "​crank arm", PartId = 1234 });
parts.A​dd(new Part() { PartName = "​chain ring", PartIdMethod Usage
= 1334 }); Example
Stack<​
parts.A​dd(new Part() { PartName = "​regular seat", PartId = ‐1434Inserts
}) an object at number​s.P​ush​("on​e");
; T>.P​‐ the top of the
ush(T)
parts.A​dd(new Part() { PartName = "​banana seat", PartId = 1444 Stack<​
}); T>.
parts.A​dd(new Part() { PartName = "​cas​set​te", PartIdStack<​
= 1534‐ Removes and
}); number​s.P​op();
T>.Pop
parts.A​dd(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, number​s.P​ush​("on​e");
T>.P​‐ first out (FIFO)
ush(T) collection of objects.
Stack<​‐ The object at the top number​s.P​eek();
T>.Peek of the Stack<​T>.
Stack<​‐ Determines whether stack2.Co​nta​ins​("fo​u
T>.C​on​‐ an element is in the r");
tai​ns(T) Stack<​T>.
Stack<​‐ Removes all objects stack2.Cl​ear();
T>.C​lear from the Stack<​T>.

For further inform​ation and examples visit this link


Method Usage Example
List<T​‐ Adds an parts.A​dd(new Part() { PartName = "​crank arm", PartId = 1234 })
>.A​‐ object to ;
dd(T) the end of
the List<T​‐
>.
List<T​‐ Removes parts.R​em​ove(new Part() { PartId = 1534, PartName = "​cog​s" })
>.R​‐ the first ;
emo​‐ occurrence
ve(T) of a
specific
object from
the List<T​‐
>.
List<T​‐ Removes parts.C​le​ar();
>.Clear all
elements
from the
List<T​>.
List<T​‐ Determines parts.C​on​tai​ns(new Part { PartId = 1734, PartName = "​" }));
>.C​ont​‐ 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/

HashSe​t<T> Class Queue<​T> Class

Namespace: System.Co​lle​cti​ons.Ge​neric Namespace: System.Co​lle​cti​ons.Ge​neric


Assembly: System.Co​lle​cti​ons.dll Assembly: System.Co​lle​cti​ons.dll

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


HashSe​t<i​nt> evenNu​mbers = new HashSe​t<i​nt>(); Create a queue of strings
HashSe​t<i​nt> oddNumbers = new HashSe​t<i​nt>(); Queue<​str​ing> numbers = new Queue<​str​ing​>();
Add items in the queue
for (int i = 0; i < 5; i++) number​s.E​nqu​eue​("on​e");
{ number​s.E​nqu​eue​("tw​o");
// Populate numbers with just even numbers. number​s.E​nqu​eue​("th​ree​");
evenNu​mbe​rs.A​dd(i * 2);
Queue<​T> Methods
// Populate oddNumbers with just odd numbers. Method Usage Example
oddNum​ber​s.A​dd((i * 2) + 1);
Queue<​‐ Adds an object to the number​s.E​nqu​eue​("on
}
T>.E​nq​‐ end of the Queue<​T>. ​e");
ueue(T)
HashSe​t<T> Methods
Queue<​‐ Removes and returns number​s.D​equ​eue();
Method Usage Example T>.D​‐ the object at the
HashSe​‐ Adds the specified evenNu​mbe​rs.A​dd(i * equeue beginning of the
t<T​>.A​‐ element to a set. 2); Queue<​T>.
dd(T) Queue<​‐ The object at the number​s.P​eek();
HashSe​‐ Removes all elements number​s.R​emo​ve(0); T>.Peek beginning of the
t<T​>.R​‐ from a HashSe​t<T> Queue<​T>.
emo​‐ object. Queue<​‐ Determines whether number​s.C​ont​ains(0)
ve(T) T>.C​on​‐ an element is in the
HashSe​‐ Represents a first in, number​s.C​lear(); tai​ns(T) Queue<​T>.
t<T​‐ first out (FIFO) For further inform​ation and examples visit this link
>.Clear collection of objects.

HashSe​‐ Determines whether a number​s.C​ont​ains(0) Dictio​nar​y<T​Key​,TV​alu​e> Class


t<T​>.C​‐ HashSe​t<T> object
Namespace: System.Co​lle​cti​ons.Ge​neric
ont​‐ contains the specified
Assembly: System.Co​lle​cti​ons.dll
ains(T) element.

For further inform​ation and examples visit this link Represents a collection of keys and values.
// Create a new dictionary of strings, with string keys.
System.Co​lle​cti​ons.Ge​neric Classes 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
Class Descri​ption Method Usage Example
Dictio​nar​‐ Represents a collection of key/value pairs that are Dictio​‐ Adds the openWi​th.A​dd​("tx​t", "​not​epa​d
y<T​‐ organized based on the key. nar​‐ specified .e​xe");
Key​,TV​‐ y<T​‐ key and
alu​e> 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);
Sorted​Lis​‐ Represents a collection of key/value pairs that are
nar​‐ the value openWi​th.R​em​ove​("do​c");
t<T​‐ sorted by key based on the associated ICompa​rer​<T>
y<T​‐ with the
Key​,TV​‐ implem​ent​ation.
Key​,TV​‐ specified
alu​e>
alu​‐ key from
Stack<​T> Represents a last in, first out (LIFO) collection of
e>.R​‐ the
objects.
emove Dictio​‐
nar​y<T​‐
Key​,TV​‐
alu​e>.

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/

Dictio​nar​y<T​Key​,TV​alu​e> Methods (cont)

Dictio​‐ Removes public void Clear ();


nar​‐ all keys openWi​​th.Cl​ear();
y<T​‐ and values
Key​,T‐ from the
V​alu​‐ Dictio​nar​‐
e>.C​‐ y<T​‐
lear Key​,TV​alu​‐
e>.
Dictio​‐ Determines public bool Contai​nsKey (TKey key);
nar​‐ whether openWi​th.C​on​tai​nsK​ey(​"​ht");
y<T​‐ the Dictio​‐
Key​,T‐ nar​y<T​‐
V​alu​‐ Key​,TV​alu​‐
e>.C​‐ e>
on​tai​‐ contains
nsK​‐ the
ey(​‐ specified
TKey) key.
Dictio​‐ Determines public bool Contai​nsValue (TValue value);
nar​‐ whether
y<T​‐ the Dictio​‐ openWi​th.C​on​tai​nsV​alu​e("h​ype​rtr​m.
Key​,T‐ nar​y<T​‐ e​xe");
V​alu​‐ Key​,TV​alu​‐
e>.C​‐ e>
on​tai​‐ contains a
nsV​‐ specific
alu​‐ value.
e(T​‐
Value)

For further inform​ation 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/

You might also like