Convert dictionary, string. A Dictionary can be converted to string format. This string can then be persisted in a text file and read back into memory. A new Dictionary instance is then constructed. Both the keys and the values are persisted.

Example. Here we look at a simple way you can write the values of Dictionary types to the disk and then parse them. I had to store a Dictionary of string keys and int values. This is also called serialization.

Tip: There are advanced methods in .NET you can use. However, if you want exact control, my method may be best.

Next, we must get a new Dictionary<string, int> from the file we saved it to. GetDict here does that for us. First, it declares the Dictionary it will return. It reads in the specified file with .NET file handling.

Split. The string has both commas and semicolons that separate parts. We split on both characters and then consume two results in each loop iteration. We use ContainsKey and Add on the Dictionary we are building. This reliably works.

Exceptions. This code will crash but that's not my fault. Whenever you deal with the file system, your input may become corrupted. It will fail. So deal with as best you can with exceptions. The two calls above should be wrapped in exceptions.

Recover from errors. Whenever you deal with the file system and your code is not critical, try to recover from any errors. Recover by returning an error string, blank string, or just doing nothing.

Summary. We persisted the Dictionary to the disk and read it back in from the file. You can sometimes develop custom methods to do this with the best performance. This method is easily extended to other languages.