default value for the specified type (string in this case, hence the default value is null
).
When you use the foreach
statement on a Dictionary
object to iterate over all the elements in it, each Dictionary object element is retrieved as a KeyValuePair
object:
foreach (KeyValuePair<int, string> Emp in employees)
Console.WriteLine('{0} - {1}', Emp.Key, Emp.Value);
Here's the output from these statements:
1001 - Margaret Anderson
1002 - Howard Mark
1003 - John Smith
1004 - Brian Will
To get all the keys in a Dictionary
object, use the KeyCollection
class:
//---get all the employee IDs---
Dictionary<int, string>.KeyCollection
EmployeeID = employees.Keys;
foreach (int ID in EmployeeID)
Console.WriteLine(ID);
These statements print out all the keys in the Dictionary object:
1001
1002
1003
1004
If you want all the employees' names, you can use the ValueCollection
class, like this:
//---get all the employee names---
Dictionary<int, string>.ValueCollection
EmployeeNames = employees.Values;
foreach (string emp in EmployeeNames)
Console.WriteLine(emp);
You can also copy all the values in a Dictionary
object into an array using the ToArray()
method:
//---extract all the values in the Dictionary object
// and copy into the array---
string[] Names = employees.Values.ToArray();
foreach (string n in Names)
Console.WriteLine(n);
To remove a key from a Dictionary
object, use the Remove()
method, which takes the key to delete:
if (employees.ContainsKey(1006)) {
employees.Remove(1006);
}
To sort the keys in a Dictionary
object, use the SortedDictionary<K,V>
class instead of the Dictionary<K,V>
class:
SortedDictionary<int, string> employees =
new SortedDictionary< int, string>();
Stacks
A stack is a last in, first out (LIFO) data structure — the last item added to a stack is the first to be removed. Conversely, the first item added to a stack is the last to be removed.
In .NET, you can use the Stack
class (or the generic equivalent of Stack<T>
) to represent a stack collection. The following statement creates an instance of the Stack
class of type string
:
Stack<string> tasks = new Stack<string>();
To add items into the stack, use the Push()
method. The following statements push four strings into the tasks stack:
tasks.Push('Do homework'); //---this item will be at the bottom of the stack
tasks.Push('Phone rings');
tasks.Push('Get changed');
tasks.Push('Go for movies'); //---this item will be at the top of the stack
To retrieve the elements from a stack, use either the Peek()
method or the Pop()
method. Peek()
returns the object at the top of the stack without removing it. Pop()
removes and returns the object at the top of the stack:
Console.WriteLine(tasks.Peek()); //---Go for movies---
Console.WriteLine(tasks.Pop()); //---Go for movies---
Console.WriteLine(tasks.Pop()); //---Get changed---
Console.WriteLine(tasks.Pop()); //---Phone rings---
Console.WriteLine(tasks.Pop()); //---Do homework---
If a stack is empty and you try to call the Pop()
method, an InvalidOperationException
error occurs. For that reason, it is useful to check the size of the stack by using the Count
property before you perform a Pop()
operation:
if (tasks.Count > 0)
Console.WriteLine(tasks.Pop());
else
Console.WriteLine('Tasks is empty');
To extract all the objects within a Stack
object without removing the elements, use a foreach statement, like this:
foreach (string t in tasks) Console.WriteLine(t);
Here's what prints out:
Go for movies
Get changed
Phone rings
Do homework
Queues
The queue is a first in, first out (FIFO) data structure. Unlike the stack, items are removed based on the sequence that they are added.
In .NET, you can use the Queue class (or the generic equivalent of Queue<T>
) to represent a queue collection. The following statement creates an instance of the Queue
class of type string:
Queue<string> tasks = new Queue<string>();
To add items into the queue, use the Enqueue()
method. The following statement inserts