using System.Collections.Generic;
public static void Main()
PersistentData.AddMessageToCollection(1, "cat");
PersistentData.AddMessageToCollection(2, "dog");
Console.WriteLine(string.Format("Element requested = {0}", PersistentData.GetMessageFromCollection(1)));
Console.WriteLine("\r\nIterating oppsed to requesting a specific element");
foreach (var element in PersistentData.GetStoredDataCollection())
Console.WriteLine(string.Format("Iterated element at key {0} = value {1}", element.Key, element.Value));
public static class PersistentData
private static Dictionary<int, string> StoredData = new Dictionary<int, string>();
public static string GetMessageFromCollection(int elementKey)
string valueToReturn = "";
if (!StoredData.ContainsKey(elementKey))
throw new KeyNotFoundException(string.Format("Element key {0} does not exist in the specified collection", elementKey));
StoredData.TryGetValue(elementKey, out valueToReturn);
catch (KeyNotFoundException knfe)
Console.WriteLine(knfe.Message);
public static void AddMessageToCollection(int elementKey, string message)
if (StoredData.ContainsKey(elementKey))
Console.WriteLine(string.Format("Collection already contains key {0}, value {1} has not been added (or updated)", elementKey, message));
StoredData.Add(elementKey, message);
public static Dictionary<int, string> GetStoredDataCollection()