Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7.0 Tuple Deduction

Tags:

c#

tuples

c#-7.0

When I write this line:

Tuple<string,string> key = (controller, action);

I get this error:

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type '(string controller, string action)' to 'System.Tuple' Project.Web PageMetadata.cs 27 Active

This seems like a fairly straight-forward, intuitive application of the new tuple enhancements that are at the heart of the C#7 updates, and yet it doesn't work. What am I doing wrong?

like image 464
Jake Avatar asked Dec 17 '17 00:12

Jake


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 ...

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.

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 C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

The new tuples features require the ValueTuple type i.e.

ValueTuple<string, string> key = (controller, action);

or

var key = (controller, action);

it's important to note that a Tuple is a class and ValueTuple is a struct. you should not mix them up. see here for more details about the new tuple features in C# 7.

like image 116
Ousmane D. Avatar answered Sep 22 '22 08:09

Ousmane D.


First of all, you get that error as you are trying to cast the new style tuple (ValueTuple) to the old style tuple (Tuple).

This can be achieved using the ToTuple() extension method:

Tuple<string,string> key = (controller, action).ToTuple();        

But that probably isn't what you are trying to do. If you want to create an instance of a new tuple, you could do:

ValueTuple<string,string> key = (controller, action);

But if you do that, you still end up with the two elements being called Item1 and Item2, which defeats one of the key features of the new tuple syntax: named elements. Change it to use var, and you then get named elements:

var key = (controller, action);
Console.WriteLine(key.controller); // key.controller is now valid

If you really don't like using var (some folk do not), then you can express it longhand to still get those named elements:

(string controller, string action) key = (controller, action); 
Console.WriteLine(key.controller);
like image 30
David Arno Avatar answered Sep 19 '22 08:09

David Arno