Why won't the Visual Studio debugger properly evaluate expressions containing generic type arguments?

In the following code:

private static void Main(string[] args) { var listy = new List<DateTime> { DateTime.Now }; MyMethod(listy); } static void MyMethod<T>(List<T> myList) { // put breakpoint here } 

If I break down in the debugger, open QuickWatch in "myList", I see:

 myList [0] Raw View 

If I select the "[0]" node and click "Add Clock", the expression added to the "Watch":

(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>(myList)).Items[0]

This expression seems correct, and yet the following error is displayed in the viewport:

The best overloaded method match for 'System.Collections.Generic.Mscorlib_CollectionDebugView.Mscorlib_CollectionDebugView (System.Collections.Generic.ICollection)' has some invalid arguments

This seems like a bug in the debugger. Why is this happening? And is it documented somewhere?

+7
source share
2 answers

This looks like an error in the logic for overloading expressions in C # expressions. The combination of calling the type constructor and passing the associated delivery is represented by the key. Removing any of them seems to fix the problem. For example, you can call an expression specified explicitly: myList to myList ICollection<DateTime> (this does not fix all the cases I tried)

Here is an example of a program I wrote to narrow down the problem

 class C<T> { public C(ICollection<T> collection) { } } static void Example<T>(ICollection<T> collection) { } 

In doing so, you can try the following ratings

  • Example(myList) - works without errors
  • new C<DateTime>(myList) - Crash with the same error

At this point, I think you should point the error to Connect . This is definitely a bug (similar code works fine in VB.Net)

+1
source

Looks like that. I was able to reproduce the error. Mscorlib_CollectionDebugView<T> has only one constructor that accepts ICollection<T> and List<T> implements ICollection<T> . In addition, listing ICollection<T> explicitly performed:

 (new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>((ICollection<DateTime>)myList)).Items[0] 
0
source

All Articles