51

We have the following code that has been working fine in our UWP app until today after we updated Visual Studio 2017 to the latest 15.3.

private void Test()
{
    var groups = new List<(Guid key, IList<(string, bool)> items)>();

    var items = new List<(string, bool)>
    {
        ("a", true),
        ("b", false),
        ("c", false)
    };
    var group = (Guid.NewGuid(), items);

    groups.Add(group);
}

There is no error message but this in the output window

Tuple element name 'items' is inferred. Please use language version 7.1 or greater to access an element by its inferred name.

Any idea why and how to fix this?

Jessica
  • 2,057
  • 1
  • 15
  • 24
  • 4
    @itsme86 well, I can't just upgrade it to use C#7.1 since it's a UWP app. – Jessica Aug 14 '17 at 23:26
  • This has nothing to do with your question as such, but types like `new List<(Guid key, IList<(string, bool)> items)>` make decent use cases *against* tuples. No operation on that except for adding items is efficient or obvious -- consider if you don't want something like `Dictionary>` instead. (Obviously the proper types depend entirely on your use case, but nested lists of tuples tend not to be it.) – Jeroen Mostert Aug 14 '17 at 23:40
  • @itsme86 Do you know how to specify language version 7.1? I'm assuming this is actually just a preview-related error message, and not yet available in non-preview Visual Studio 2017. – Lasse V. Karlsen Aug 15 '17 at 10:24
  • 2
    @Jessica moving to C# 7.1 should be just fine for a UWP application. The C# 7.1 compiler is already being used, the language version flag only controls what features are available. – JaredPar Aug 15 '17 at 16:25
  • @JaredPar can you please show me how? – Jessica Aug 15 '17 at 21:30

3 Answers3

81

Project->Properties->Build->Advanced->Language Version->C# latest Minor Version

John Stewien
  • 1,046
  • 9
  • 5
11

Looks like this is a breaking change in C# 7.1. (as pointed out by @JulienCouvreur, this is actually a bug, but the workaround below should still work though).


Workaround

Try giving a name (e.g. use the same name items from IList<(string, bool)> items to be consistent) explicitly to items (i.e. the list instance).

var group = (Guid.NewGuid(), items: items);
Justin XL
  • 38,763
  • 7
  • 88
  • 133
  • No, this is a bug. The breaking change described in the spec involves an extension method, which becomes hidden by newly named field. – Julien Couvreur Aug 16 '17 at 09:56
  • Hey @JulienCouvreur thanks for letting me know! I thought they are related but clearly you know this better than me. :) – Justin XL Aug 16 '17 at 11:30
11

This a confirmed bug, introduced in 15.3. The fix will ship as part of a servicing release (15.3.2).

The issue is tracked at https://github.com/dotnet/roslyn/issues/21518

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40
  • 2
    Do we still need to select "C# latest Minor Version" for this to work with a target Framework of .Net Framework 4.7? I have VS 2017 15.3.3 but I get the compile error unless I switch to latest minor version under the Advanced Build settings. – Steve Young Aug 30 '17 at 09:39
  • @Steve Young, The compiler doesn't know anything about target framework. The C# language rules (and in this case the bug) are determined by the "language version", but not target framework. This bug was fixed in 15.3.2, so if you can still repro on 15.3.3, please re-open the linked issue or open a new one. Thanks – Julien Couvreur Aug 30 '17 at 20:48