 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
KeyNotFoundException in C#
The KeyNotFoundException is thrown when a key you are finding is not available in the Dictionary collection.
Let us see an example −
Example
using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      try {
         var dict = new Dictionary<string, string>() {
            {"TV", "Electronics"},
            {"Laptop", "Computers"},
         };
         Console.WriteLine(dict["Pen Drive"]);
      }
      catch (Exception e) {
         Console.WriteLine(e);
      }
   }
}
The following is the output. The error KeyNotFoundException is visible since the key is not in the Dictionary −
Output
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <902ab9e386384bec9c07fa19aa938869>:0
Advertisements
                    