Why does the Visual Studio watch window show wrong values for ValueTuples in a collection?
Asked Answered
L

1

6

I'm finding that ValueTuples evaluate differently when I access their properties from a collection.

    public static List<Tuple<string, bool>> MyTupleList = new List<Tuple<string, bool>>
    {
        new Tuple<string, bool>("test", true)
    };

    public static List<(string b, bool c)> MyList = new List<(string b, bool c)>
    {
        ("test", true)
    };

Why do these two highlighted lines evaluate differently and how can I change "MyList[0].c" to get the value correctly?

enter image description here

Liverwurst answered 4/12, 2018 at 12:50 Comment(8)
How is MyTupleList related which is not a ValueTuple?Sempach
I simply included it to highlight the evaluation of the Tuple type "MyTupleList[0].Item1" working when "MyList[0].Item1" doesn't. Probably should have mentioned that in my question.Liverwurst
Can you reproduce this in code (accessing MyList[0].c yields the wrong value) or is this just a problem with the Visual Studio tool window? If the latter: This is a Visual Studio issue (and not a C# issue), so please include the precise VS version in your question and tag it with visual-studio-{version}. If the former: Please provide a full repro example. Oh, and good question, by the way!Inhabited
This seems to be an issue with the Immediate Window and other tooling (Watch Window etc) - it doesn't affect the app itself. I can repro it in VS 15.8.4, .NET Core 2.1.Pruitt
Can't repro with 15.9.3. Can't even access the property c in MyList[0].c with the debugger: "'System.ValueTuple<string,bool>' does not contain a definition for 'c' and no extension method 'c' accepting a first argument of type 'System.ValueTuple<string,bool>' could be found (are you missing a using directive or an assembly reference?) "Sempach
@Inhabited it seems to evaluate correctly if I write the line in code. So it looks like it may just be the Visual Studio tool windows. I found the issue using the C# immediate window and then replicated in the watch window. My Visual Studio version is 15.8.9. I'll add the tag.Liverwurst
@Rango I can repro it in 15.9.3. Make sure to use MyList[0].b or MyList[0].c as the Watch expression.Celebes
@PeterB: i can't use those properties as i've mentioned above. And if i use the exact code in this bug report it correctly shows 123 for Item1 and "abc" for Item2. .NET version: 4.7.03056, Language Level: C# 7.2Sempach
I
4

This seems to be a bug in Visual Studio 2017.

There are a few related bugs mentioned on Roslyn's github issue tracker, e.g.:

Since the issue tracker of Visual Studio is not public, we can only wait and hope that these bugs get fixed.

Inhabited answered 4/12, 2018 at 13:4 Comment(2)
It seems to be fixed in VS 2017 v15.9.3. Because i can't access c in the debugger and if i use Item1 or Item2 i get the correct values(so not null or false). Maybe it's still open because you can't access the named properties in the debugger.Sempach
@Rango: Good point. If you feel like it, you might want to add that as a comment to the github issue.Inhabited

© 2022 - 2024 — McMap. All rights reserved.