Download presentation
Presentation is loading. Please wait.
Published byYenny Kusumo Modified over 6 years ago
1
Data Structures and Database Applications Dictionaries in C#
2
Dictionary Overview System.Collections.Generic.Dictionary
Automatic resizing and homogeneous, unordered list Represents a collection of key-value pairs
3
The Dictionary ADT A dictionary is a data structure that stores entries. Each entry contains two parts: a key and a value. The value can be any data type. The key is usually an integer, but it doesn’t have to be. It can be any type of value that has a Hash Function which can convert it to a number. x[key] = value; v[14] = "Another Thing“; w["first"] = "Something“; We will discuss Hash Functions in more details in the next unit, however, for now you just need to know that a string can be converted to a number using a Hash Function.
4
The Dictionary ADT The key in the key value pair is also called a search key, which is used to search for the corresponding value. For example, a dictionary can be stored in a data structure where the words are the keys and the definitions of the words are the values, just like the analogous Webster’s Dictionary.
5
The Dictionary ADT A dictionary data structure can also be referred to in other applications as a map, an associative array, or a hash table. With a dictionary you essentially map entries to key values that will be useful to use when you need to locate a specific entry later. A numeric index is used in this way for an array, but something more complicated might be needed. For instance, if you have a set of people from a neighborhood in your dictionary, a string representing the Street Address might be used as the key. Each key must be unique to a single entry value in the dictionary.
6
Creating and Traversing a Dictionary
Example: Dictionary<string, string> openWith = new Dictionary<string, string>(); openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); } if (openWith.ContainsKey("bmp")) { Console.WriteLine("\nbmp is opened with {0}\n", openWith["bmp"]); foreach (KeyValuePair<string, string> item in openWith) { Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
7
Creating and Traversing a Dictionary
The previous code prints out the following: An element with Key = "txt" already exists. bmp is opened with paint.exe Key: txt, Value: notepad.exe Key: bmp, Value: paint.exe Key: dib, Value: paint.exe Key: rtf, Value: wordpad.exe
8
Common C# Dictionary Methods
Add() ContainsKey() ContainsValue() Remove() Clear()
9
Dictionary Database Implementation
To create an efficient Database implementation of a Dictionary we will continue using the Tables we created for Stacks, Queues and LinkedLists operations. We shall also add a new table called the DLists Table which will have a string Key field and an EntryID field for a DList entry. Although the Key field will always be a string in this case, the EntryID field in the DList entry could point to an Entry of any type of data in theory, but for now, just to Entries of Names, Phones and s.
10
Dictionary Database Implementation
The DLists Table:
11
Dictionary Database Implementation
The entity name the new table will be bound to is db5, like so:
12
Dictionary Database Implementation
Using db5, the following method gets a normal Entry from the DList Table for a given key:
13
Dictionary Database Implementation
Using db5, this method adds or replaces an Entry in the DList Table for a given key:
14
Dictionary Database Implementation
Using db5, this method removes an Entry in the DList Table for a given key:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.