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

Heinzi
  • 167,459
  • 57
  • 363
  • 519
Rob Powell
  • 1,367
  • 2
  • 13
  • 10
  • 1
    How is `MyTupleList` related which is not a `ValueTuple`? – Tim Schmelter Dec 04 '18 at 12:54
  • 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. – Rob Powell Dec 04 '18 at 12:56
  • 4
    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! – Heinzi Dec 04 '18 at 12:57
  • 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. – mjwills Dec 04 '18 at 13:01
  • Can't repro with 15.9.3. Can't even access the property `c` in `MyList[0].c` with the debugger: _"'System.ValueTuple' does not contain a definition for 'c' and no extension method 'c' accepting a first argument of type 'System.ValueTuple' could be found (are you missing a using directive or an assembly reference?) "_ – Tim Schmelter Dec 04 '18 at 13:02
  • @Heinzi 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. – Rob Powell Dec 04 '18 at 13:09
  • 1
    @Rango I can repro it in 15.9.3. Make sure to use `MyList[0].b` or `MyList[0].c` as the Watch expression. – Peter B Dec 04 '18 at 13:11
  • @PeterB: i can't use those properties as i've mentioned above. And if i use the exact code in this [bug report](https://github.com/dotnet/roslyn/issues/28151) it correctly shows `123` for `Item1` and `"abc"` for `Item2`. .NET version: 4.7.03056, Language Level: C# 7.2 – Tim Schmelter Dec 04 '18 at 13:12

1 Answers1

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 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. – Tim Schmelter Dec 04 '18 at 13:07
  • @Rango: Good point. If you feel like it, you might want to add that as a comment to the github issue. – Heinzi Dec 04 '18 at 13:18