'An item with the same key has already been added.' -- Which key?

Viewed 4600

The following code:

var rnd = new Random();
Enumerable.Range(1,20).Select(x => rnd.Next(10)).ToDictionary(x => x, x => "");

will inevitably fail with the following exception:

System.ArgumentException: 'An item with the same key has already been added.'

Is there any way to use the Visual Studio IDE while debugging, to find out the attempted key? Is the key stored somewhere in the exception details -- some internal / private field perhaps; which can be read using the exception details window?

I can't change the source of ToDictionary to include the key in the thrown exception.

Note: I am aware that I can rewrite this in a debuggable fashion, using a For Each loop instead of LINQ. But I am asking about the general case -- when this exception is thrown, and the source code is not under my control, is there some way to access the key?

Using VS 2017 Community Edition.

2 Answers
Related