• Register
0 votes

Problem :

I am new to C#. I want to know how can I get the value of a given key in a following exception which I am currently facing in C# in a way it affects all the generic classes?

Below is the exception which I am currently facing.

"The given key was not present in the dictionary."
How  to fix above exception.
8 4 2
2,300 points

1 Answer

0 votes

Solution :

I have faced such issues earlier. I can tell that you can try to set your debugger to break at a point exactly where your exception is thrown at a beginning. By doing this a key which was not present earlier will be accessible to you as the value in your call stack.

If you are using the Visual Studio then in your Visual Studio you can find this option as shown below :

Debug → Exceptions... → Common Language Runtime Exceptions → System.Collections.Generic

At this path must check a Thrown check box.

If you want to address more specific instances where the information is needed by you at runtime then you need to provide your code with the use of IDictionary<TKey, TValue> and it will not be tied directly to your Dictionary<TKey, TValue> also you can try to implement your own dictionary class which will provide you the required behavior.

5 2 1
4,980 points

Related questions

1 vote
1 answer 11 views
Problem: I wrote a python program to check whether a given key already exists in a dictionary. The dictionary I have is: (If exists, print Key is present and the value of the key. Otherwise print key is not present .) {'a': 233, 'b':588, 'c':967 ... checkKey() missing 1 required positional argument: 'key' Whats the problem with my program can any one help me to sort this out? Thanks in advance.
asked Apr 26 samhaz 5k points
0 votes
1 answer 295 views
Problem : Facing following python dictonary issue. the elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 43 views
Problem : I am unable to understand the root cause of following error, because when I attach the debugger it will not occur. Invalid operation exception: collection was modified; enumeration operation may not execute. This is the WCF server in a Window based service. The method ... , the code executes fine. Do you see a problem in my code? Do I need to make the dictionary the thread-safe?
asked Nov 24, 2019 alecxe 7.5k points
1 vote
1 answer 41 views
Problem : I was trying to check if key exists in dictionary python. So I wrote this code: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False ... unindent does not match any outer indentation level What is the problem in this program and how can I check if key exists in dictionary?
asked Apr 26 samhaz 5k points
1 vote
1 answer 26 views
Problem: I am starting writing code in python. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "Present" else: print "Thanks" If there any other way to check this, please let me know.
asked Mar 10 Gavin 15.3k points