Questions tagged [valuetuple]

This covers the ValueTuple struct used by C#'s tuples

Value tuples are tuple types introduced in the .NET Framework 4.7 to provide the runtime implementation of tuples in C#. They are different from tuple classes, like Tuple<T1>, because they are value types, rather then reference ones.

141 questions
3
votes
0 answers

I thought ValueTuple was built-in to .NET Standard 2.0?

I have a .NET Framework 4.7.2 website running with a .NET Standard 2.0 Class Library DLL. I thought I understood ValueTuple and .NET Standard 2.0. Apparently I don't. Here's a ValueTuple defined in a .NET Standard 2.0 DLL. public static…
Robert4Real
  • 1,332
  • 5
  • 21
  • 39
3
votes
2 answers

In C# 7, How Can I Make a New ValueTuple from an Existing and New Elements?

In C# 7 we have value tuples of the form (a, b, c, ...) and deconstruction of them (via var (a, b, c, ...) = (a, b, c, ...)). I have a tuple of the form (row: , column: ), and I want to create a new tuple of the form (row: , column:…
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
3
votes
1 answer

Custom names in a named tuple definition becoming default names "Item1" and "Item2"

I am creating a list of named tuples. I propulate this list using the add method but after this process, I realize that the tuples' members have lost their custom names being accessible only by "Item1", "Item2"... I am using .NET framework version…
3
votes
1 answer

Is there a shorthand way to convert a Tuple to a ValueTuple?

I have just included a DLL file into my project and am using a method which returns a List. I have only recently became aware of the ValueTuple datatype in C# 7 and want to use it instead of the normal Tuples which are returned. I am aware…
James B
  • 33
  • 4
3
votes
1 answer

Deconstruct tuple from ref variable

I have the folloving method. private ref (int, int) GetValue() { var array = new (int, int)[1]; return ref array[0]; } The following code works fine: var (s1, s2) = GetValue(); But i need to use the ref feature. The following code also…
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
3
votes
3 answers

Pass ValueTuple instead of arguments

Is it possible to do in this way (or maybe I need specific version of C#)? Function triSum = (a,b,c) => {return a+b+c;}; var tup = (1,2,3); triSum(tup); //passing one tuple instead of multiple args Update: I mean passing tuple…
Troll the Legacy
  • 675
  • 2
  • 7
  • 22
3
votes
3 answers

How to create a .NET C# ValueType Tuple dynamically?

I would like to create a value type tuple dynamically from say a collection of values. Example: I have a given IEnumerable and I would like to create a tuple based on that collection. How can I achieve that? It seems that the access within a…
Natalie Perret
  • 8,013
  • 12
  • 66
  • 129
3
votes
2 answers

convert pair of arrays to array of pairs (using LINQ if possible)

I have some types S and T (e.g. S=object and T=string). I have a pair of arrays of these types, i.e. (S[], T[]) pairOfArrays; and I want to convert it to an array of pairs, i.e. (S, T)[] arrayOfPairs; How do I do that? I am most interested in…
Kjara
  • 2,504
  • 15
  • 42
3
votes
1 answer

RuntimeBinderException when accessing ValueTuple item via dynamic

I am facing a really weird behavior of ValueTuple passed from another assembly, accessed using dynamic. This code under specific circumstances throws RuntimeBinderException at the last line: TupleTest(out var t); (dynamic i1, dynamic i2) =…
LOST
  • 2,956
  • 3
  • 25
  • 40
3
votes
1 answer

Creating subtuple from a valuetuple

Is there a functionality for C# 7 ValueTuple similar to Python's slicing? The syntax for value tuples in C# is similar to Python but I can't find an elegant way to get subtuple from tuple for example. In Python 3: tuple = (1,2,3) subtuple = t[:2] …
SENya
  • 1,083
  • 11
  • 26
3
votes
4 answers

Can I pass a ValueTuple to a method that requires a generic type and still maintain the member variables?

The following is just a question about the language itself, I started thinking about this before releasing a better way to architect my code but it got me interested. If I have a method of this structure private void Foo(T bar){ //do stuff…
Geesh_SO
  • 2,156
  • 5
  • 31
  • 58
3
votes
1 answer

Caliburn.Micro - Binding ObservableCollection of ValueTuple to ComboBox

I'm trying to bind ObservableCollection of ValueTuples to ComboBox in WPF using Caliburn.Micro framework MVVM. When I do that in ViewModel: private ObservableCollection> databasesFromDisk; public…
3
votes
0 answers

Unit Testing ValueTuple with Microsoft Fakes

I am trying to use ValueTuples (current NuGet package ), and while most things I need it for have been seamless an issue has come up when trying to Unit Test certain scenarios related to inheritance. I have made a simple Interface/Implementation…
2
votes
1 answer

When TValue of Dictionary<,> is ValueTuple, does it mean that the Value of an item in the dictionary can no longer be modified once intialized?

(.NET 4.8 / C# 7.3) var test = new Dictionary { { "a", (0, "A") } , { "b", (1, "B") } , { "c", (2, "C") } }; test["c"].Item1 = 3; The last line gives me CS1612: Cannot modify the return value of…
Jyunhao Shih
  • 1,287
  • 2
  • 8
  • 9
2
votes
1 answer

Is there a way to name the fields when receiving a ValueTuple as a parameter?

There is a method that receives a ValueTuple and returns it after modifying it. In this case, can I specify the field name of the ValueTuple in the method parameter? private static void Operation() { var tuple = (X: 10, Y: 20); var…
isakgo_
  • 750
  • 4
  • 15