Lecture 7 Collectionsin C
Lecture 7 Collectionsin C
Lecture 7 Collectionsin C
net/publication/352837988
Collections in C#
CITATIONS READS
0 1,509
1 author:
Tarfa Hamed
University of Mosul
38 PUBLICATIONS 263 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tarfa Hamed on 30 June 2021.
Advanced Programming in C#
Lecture Seven
C# - Collections
1
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Hashtable
SortedList
Stack
Queue
BitArray
3
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
myArray[0] = 101;
myArray[1] = 102;
myArray[2] = 103;
myArray[3] = 104;
4
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
ArrayList
myArrayList.Add(10);
myArrayList.Add(20);
myArrayList.Add(30);
myArrayList.Add(40);
1. Capacity
5
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
3. IsFixedSize
6
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
ArrayList Example
al.Add(45);
al.Add(78);
al.Add(33);
al.Add(56);
al.Add(12);
al.Add(23);
al.Add(9);
7
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Console.Write("Content: ");
Console.WriteLine();
al.Sort();
8
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
ArrayList Example
Capacity: 8
Count: 7
Content: 45 78 33 56 12 23 9
Content: 9 12 23 33 45 56 78
• This means an item of any type can be added to ArrayList, which later
could result in run-time exception.
myArrayList.Add(10);
myArrayList.Add(20);
myArrayList.Add(30);
myArrayList.Add(40);
9
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Generic Collections
• Meaning that you can only put one type of object into it.
10
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• List
• Dictionary
• Queue
• Stack
• Linked List
• Sorted List
• Sets
• TKey denotes the type of key and TValue is the type of value.
11
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
// Or
Dictionary Example
{1, "One"},
{2, "Two"},
12
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
{3, "Three"}
};
Property Description
Gets the total number of elements exists in the
Count
Dictionary<TKey,TValue>.
Returns a boolean indicating whether the
IsReadOnly
Dictionary<TKey,TValue> is read-only.
Method Description
void Add(TKey key, Add key-value pairs in Dictionary<TKey,
TValue value) TValue> collection.
Removes the first occurance of specified item
void Remove(T item)
from the Dictionary<TKey, TValue>.
void Remove(TKey) Removes the element with the specified key.
bool
Checks whether the specified key exists in
ContainsKey(TKey
Dictionary<TKey, TValue>.
key)
13
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
bool
Checks whether the specified key exists in
ContainsValue(TValue
Dictionary<TKey, TValue>.
value)
Removes all the elements from
void Clear()
Dictionary<TKey, TValue>.
Example:
myDictionary.Add(1, "One");
myDictionary.Add(2, "Two");
myDictionary.Add(3, "Three");
• Specify key (not index) to get a value from a dictionary using indexer
like an array.
14
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Console.WriteLine(myDictionary[1]); //
returns One
Console.WriteLine(myDictionary[2]); //
returns Two
if (myDictionary.ContainsKey(2))
// do something
15
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Example:
};
16
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• Meaning the item that is put first in the queue is read first.
• The queue just allows you to add an item to it, which is put at the
end of the queue (with the Enqueue method), and to get items
from the beginning of the queue (with the Dequeue method).
17
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• The Enqueue method adds items to one end of the queue; the items
are read and removed at the other end of the queue with the
Dequeue method.
• Invoking the Dequeue method once more removes the next item
from the queue.
Queue<T> Members
Members Description
Count Returns the number of items in the queue.
Enqueue Adds an item to the end of the queue.
Reads and removes an item from the head of the
queue. If there are no more items in the queue when
Dequeue
the Dequeue method is invoked, an exception of type
InvalidOperationException is thrown.
Reads an item from the head of the queue but does
Peek
not remove the item.
• Use the Enqueue() method to add an element to the end of the queue.
• The following example adds int values into a Queue<T> of int type.
18
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
myQueue.Enqueue(10);
myQueue.Enqueue(20);
myQueue.Enqueue(30);
myQueue.Enqueue(40);
• Use the Peek() method to read the element from the head of the
queue, without removing it.
• Use the Count property to get the total number of elements in the
queue.
19
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Console.WriteLine(item);
Example:
myQueue.Enqueue(10);
myQueue.Enqueue(20);
myQueue.Enqueue(30);
myQueue.Enqueue(40)
foreach(int i in myQueue)
Console.WriteLine(i);
10
20
30
40
20
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Stack<T> Members
Members Description
Returns the number of items
Count
in the stack.
Adds an item on top of the
Push
stack.
Removes and returns an item
from the top of the stack. If
the stack is empty, an
Pop
exception of type
InvalidOperationException
is thrown.
21
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
• Use the Push() method to add an element to the top of the stack.
myStack.Push(10);
myStack.Push(20);
myStack.Push(30);
myStack.Push(40);
• Use the Pop() method to remove an element from the top of the
stack.
• Use the Peek() method to read the element from the top of the
stack, without removing it.
• Use the Count property to get the total number of elements in the
stack.
Console.WriteLine(item);
Example:
myStack.Push(10);
myStack.Push(20);
myStack.Push(30);
myStack.Push(40);
foreach(int i in myStack)
Console.WriteLine(i);
}
23
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
40
30
20
10
Write a program that inputs a line of text and uses a stack object to
display the line reversed.
Exercise: Palindromes:
Write a program that reads a positive integer and prints the binary
representation of that integer.
Hint: Divide the integer by 2 and push the remainder onto stack.
24