Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7 Tuples and names in .NET Core

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

like image 480
Ondrej Tomcik Avatar asked May 08 '17 08:05

Ondrej Tomcik


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

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' :

Install missing package

The Find this type on nuget.org menu is a similar ReSharper feature

like image 128
Panagiotis Kanavos Avatar answered Oct 04 '22 03:10

Panagiotis Kanavos