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
5
votes
1 answer

How can I use tuples in nunit TestCases?

I am trying to specify nUnit testCases using tuples, but I get a compiler error in VisualStudio. This simple example demonstrates what I am trying to do: [TestCase((1, 2), (3, 5))] public void TestRangeOverlaps((int start, int end)…
GarethOwen
  • 6,075
  • 5
  • 39
  • 56
5
votes
1 answer

F# generics generic construct requires that the type 'struct (Guid * int)' have a public default constructor

I have a interface in C# with method with this return type: Task<(Guid, int)?> I need to implement this interface in F# and if I am not mistaken this should be equivalent in F#: Task>> Unfortunately when I compile I…
Michal
  • 1,324
  • 2
  • 16
  • 38
5
votes
1 answer

How do I use the ValueTuple naming feature with anonymous methods?

I would like to use the naming feature of ValueTuple as follows: IEnumerable<(string, char, int)> valueTuples = new(string, char, int)[] { ("First", '1', 1), ("Second", '2', 2), ("Third", '3', 3) }; var…
mikevg
  • 53
  • 4
4
votes
3 answers

Value tuples exist, so why use the "out" parameter modifier?

Microsoft's documentation for the out parameter modifier points out the following: Declaring a method with out arguments is a classic workaround to return multiple values. Consider value tuples for similar scenarios. This strikes me as a…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
4
votes
1 answer

How to create a dictionary with a 3-dimensional key

I need some kind of structure that works like a dictionary, but the key is a 3D point (represented by 3 integers). Ball b1 = new Ball(); Ball b2 = new Ball(); D[-1,3,29] = b1; D[9,0,3] = b2; I'm thinking about creating something like…
Daniel
  • 7,357
  • 7
  • 32
  • 84
4
votes
1 answer

Should we use Tuple or ValueTuple for reference-type only variables?

There's many questions contains only difference between Tuple(class) vs ValueTuple(struct) All gone with ValueTuple where there's no GC pressure. But I don't get enough with why ValueTuple accepts all data-types then? why not just ValueTuple accepts…
deveton
  • 291
  • 4
  • 26
4
votes
4 answers

C# - deserialize JSON into ValueTuple

I'm trying to deserialize [{"foo": "1", "bar": false}, {"foo": "2", "bar": false}] into List<(string, bool)> type: JsonConvert.DeserializeObject>(json) But always get a list of default values - (null, false). …
anatol
  • 1,680
  • 2
  • 24
  • 47
4
votes
1 answer

ValueTuple set fields via reflection

I'm trying to set the fields of a ValueTuple at runtime using reflection and am having an issue. I suspect I'm not understanding something about how FieldInfo.SetValue works. var a = (5, 5); a.Item1 = 6; Console.WriteLine(a.Item1); // 6 var t =…
MgSam
  • 12,139
  • 19
  • 64
  • 95
4
votes
1 answer

Casting a ValueTuple from a cached object

When I am checking my cache to see if if my ValueTuple is in the cache, I use the code below. In this scenario, the value coming back is null (aka does not exist in the cache). When the code runs I get object not set to instance of an object error…
Chris Auer
  • 1,405
  • 12
  • 23
4
votes
6 answers

c# 7.2 default expression and Equals (bug?)

I'm using Visual Studion 2017 version 15.5.2, and C# version 7.2. To the point: Color c = default; // or: c = default(Color); no difference Debug.Print($"{c.Equals(default(Color))}"); //…
lisz
  • 435
  • 5
  • 9
4
votes
1 answer

ValueTuple With WPF Binding

why binding to ValueTuple property members (like Item1, Item2 ect) dont work? the code: txtTest.DataContext = ("Item A", "Another Item.."); output window: BindingExpression path error:…
dovid
  • 6,354
  • 3
  • 33
  • 73
4
votes
1 answer

is the ValueTuple structure only mutable as variable?

I played around with the ValueTuple structure and tried to implement an immutable composite key. The key is composed of value types. I tried to break the following implementation with some unit tests, so far without success. Am I missing…
Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
3
votes
1 answer

Using tuples in C#, (A, B, C) vs Tuple

I've created a method: public static Tuple SplitStr(string req) { //... return (A, B, C) } it complaint that "CSOOZQ: Cannot implicitly convert type '(strinq _keyword, strinq _filterCondition, strinq…
EBDS
  • 1,244
  • 5
  • 16
3
votes
0 answers

How to deconstruct ValueTuples in Visual Basic .NET?

Let the following C# code : static (int, string) FunctionThatReturnsTwoValues() { return (123, "hello"); } public static void Main(string[] args) { (var someInt, var someString) = FunctionThatReturnsTwoValues(); …
Virus721
  • 8,061
  • 12
  • 67
  • 123
3
votes
1 answer

Why are there hidden copies created by the compiler when comparing C# Value Tuples with ==?

Equality between Value Tuple types was introduced in C# 7.3. It allows code like this: var x = (1, 2); var y = (1, 2); if(x == y) ... This works fine and gives the correct result. However, the compiler introduces hidden copies of the tuple…
SteveLove
  • 3,137
  • 15
  • 17
1 2
3
9 10