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?
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.
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.
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.
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.
ValueTuple
instead:ValueTuple test = TupleTest();
ToTuple
extension method (ToValueTuple
is also available):Tuple test = TupleTest().ToTuple();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With