Reputation: 1210
With C# 7 new Tuple feature we should be able to access fields by it's names derived from the type.
public (double lat, double lng) GetLatLng(string address) { ... }
var ll = GetLatLng("some address");
Console.WriteLine($"Lat: {ll.lat}, Long: {ll.lng}");
This is not possible in .NET Core. Why? -> Works only with Item1; Item2. Not with .lat .lng.
Thanks
Upvotes: 10
Views: 8934
Reputation: 131189
UPDATE
Visual Studio 2017 Intellisense may be slow to update itself after adding the System.ValueTuple
package and keep displaying error squigglies even when there is no compilation error. Compiling the project though shows that named tuples are working. A quick fix is to re-open the source file or solution.
ORIGINAL
The error message explains that 'Predefined type System.ValueTuple'2 is not defined or imported
. You need to add the System.ValueTuple package from NuGet in order to use named tuples.
Once you add the package, the code compiles:
class Program
{
static (double lat, double lng) GetLatLng(string address)
{
return (1, 1);
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var ll = GetLatLng("some address");
Console.WriteLine($"Lat: {ll.lat}, Long: {ll.lng}");
}
}
Scott Hanselman shows how to configure Visual Studio 2017 to automatically suggest NuGet packages for missing types by enabling the settings in Options > Text Editor > C# > Advanced > Using Directives
.
After you enable the Suggest usings for types in NuGet packages
setting, the Quick Fix menu for the missing tuples shows Install package 'System.ValueTuple'
:
The Find this type on nuget.org
menu is a similar ReSharper feature
Upvotes: 9