C # 7 Tuples and Names in .NET Core

With the new Tuple C # 7 function, we need to have access to the fields by these names obtained 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. What for? Works only with Item1; Element 2. Not with .lat.lng.

thank

+4
source share
1 answer

UPDATE

Visual Studio 2017 Intellisense may slowly update after adding a package System.ValueTupleand continue to display squigglies errors, even if there is no compilation error. Compiling the project shows that named tuples work. A quick fix is ​​to reopen the source file or solution.

ORIGINAL

, " System.ValueTuple'2 is not defined or imported. System.ValueTuple NuGet, .

:

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}");
    }
}

, Visual Studio 2017, NuGet , Options > Text Editor > C# > Advanced > Using Directives.

Suggest usings for types in NuGet packages Install package 'System.ValueTuple':

Install missing package

Find this type on nuget.org - ReSharper

+4

Source: https://habr.com/ru/post/1676634/


All Articles