Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting new tuple to old one gives compilation error

Tags:

c#

tuples

c#-7.0

With the code below,

class Program
{
    static void Main(string[] args)
    {
        Tuple<int, int> test = TupleTest();

    }

    static (int, int) TupleTest()
    {
        return (1, 2);
    }

I am getting following compile time error.

Error CS0029 Cannot implicitly convert type '(int, int)' to 'System.Tuple < int, int >'

Does it mean the new version of Tuple is not compatible with the old version implicitly? Or am I doing something wrong here?

like image 948
jim crown Avatar asked Apr 23 '17 21:04

jim crown


People also ask

What is the difference between tuple and ValueTuple?

ValueTuple provides more flexibility for accessing the elements of the value tuples by using deconstruction and the _ keyword. But Tuple cannot provide the concept of deconstruction and the _ keyword. In ValueTuple the members such as item1 and item2 are fields. But in Tuple, they are properties.

What is System ValueTuple?

A ValueTuple is a structure introduced in C# 7.0. A ValueTuple overcomes two major limitations of tuples—namely, that they are inefficient and that they must be referenced as Item1, Item2, etc. That is, ValueTuples are both performant and referenceable by names the programmer chooses.

Can a tuple have a null value?

StreamBase supports the use of null values in applications. Nulls can be used to explicitly represent data that is missing or unknown. Use the reserved value null to indicate that the value of a tuple field is null.

What is AC tuple?

C# tuple is a data structure that provides an easy way to represent a single set of data. The System. Tuple class provides static methods to create tuple objects.


1 Answers

  • Use ValueTuple instead:

ValueTuple test = TupleTest();

  • Use the ToTuple extension method (ToValueTuple is also available):

Tuple test = TupleTest().ToTuple();

like image 99
Shimmy Weitzhandler Avatar answered Oct 04 '22 15:10

Shimmy Weitzhandler