The indexer throws an exception if the requested key is not in the dictionary.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
Dictionary<string, string> openWith = new Dictionary<string, string>();
openWith.Add("A", "a");
openWith.Add("B", "b");
openWith.Add("C", "c");
try
{
Console.WriteLine( openWith["NotExist"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
}
}
}
Related examples in the same category