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
103
votes
22 answers

Could not load file or assembly 'System.ValueTuple'

I've got a VS2017 project that compiles to a DLL which is then called by an EXE written by someone else. Both projects target .Net Framework 4.6.2. I rewrote one of my DLL methods to return a tuple and also imported the associated NuGet package.…
Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
98
votes
11 answers

When to use: Tuple vs Class in C# 7.0

Before Tuples, I used to create a class and its variables, then create object from this class and make that object the return type for some functions. Now, with tuples, I can do the same thing, and in C# 7.0 we can assign understandable names for…
Madonna Remon
  • 1,149
  • 1
  • 7
  • 12
43
votes
4 answers

How to create a List of ValueTuple?

Is it possible to create a list of ValueTuple in C# 7? like this: List<(int example, string descrpt)> Method() { return Something; }
ArthNRick
  • 925
  • 1
  • 7
  • 22
41
votes
5 answers

Check if value tuple is default

How to check if a System.ValueTuple is default? Rough example: (string foo, string bar) MyMethod() => default; // Later var result = MyMethod(); if (result is default){ } // doesnt work I can return a default value in MyMethod using default syntax…
nawfal
  • 70,104
  • 56
  • 326
  • 368
39
votes
1 answer

What makes ValueTuple covariant?

This compiles correctly in C# 7.3 (Framework 4.8): (string, string) s = ("a", "b"); (object, string) o = s; I know that this is syntactic sugar for the following, which also compiles correctly: ValueTuple s = new ValueTuple
Heinzi
  • 167,459
  • 57
  • 363
  • 519
37
votes
2 answers

C# 7.0 Value Tuple compile error?

When I am trying to compile the following code: var post = iPostService.GetAll().Select(x => (x.Title, x.Author)); I get the compiler error: 'An expression tree may not contain a tuple literal.' So I also tried this: var post =…
Tan Sang
  • 1,897
  • 1
  • 16
  • 28
30
votes
3 answers

ValueTuples lose their property names when serialized

While trying to serialize a named value tuple to JSON string, it loses the names assigned to items (string type, string text) myTypes = ("A", "I am an animal"); var cnvValue = JsonConvert.SerializeObject(myTypes); I am expecting the serialized…
mdowes
  • 592
  • 7
  • 18
27
votes
2 answers

Are ValueTuples suitable as dictionary keys?

I'm thinking this could be a convenient dictionary: var myDict = new Dictionary<(int, int), bool>(); What would the hashes look like? What would the equivalent key type (struct) look like?
Steinbitglis
  • 2,482
  • 2
  • 27
  • 40
25
votes
3 answers

Is it possible to bind to a ValueTuple field in WPF with C#7

If I have a viewmodel property public (string Mdf, string MdfPath) MachineDefinition { get; set; } and I try to bind to it in XAML / WPF
23
votes
1 answer

Using `is` operator with value type tuples gives error

I am trying to check if an object variable is (int, int) and if so I will use the casted variable so I have tried the codes below: //this one gives the error public void MyMethodWithIs(object val) { if(val is (int id, int name) pair) { …
ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
19
votes
1 answer

What changed in System.ValueTuple 4.4.0 -> 4.5.0?

I consider updating my System.ValueTuple references from 4.4.0 to (current) 4.5.0. To avoid regressions, I'd like to find out what changed between those two releases. The nuget page says: Release…
Heinzi
  • 167,459
  • 57
  • 363
  • 519
19
votes
3 answers

C# ValueTuple with disposable members

Let's say I have a method foo which returns a ValueTuple where one of it's members is disposable, for example (IDisposable, int). What is the best way to make sure the returned disposable object is correctly disposed on the calling side? I tried the…
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
17
votes
5 answers

Detect if an object is a ValueTuple

I have a use case where I need to check if a value is a C# 7 ValueTuple, and if so, loop through each of the items. I tried checking with obj is ValueTuple and obj is (object, object) but both of those return false. I found that I could use…
James Esh
  • 2,219
  • 1
  • 24
  • 41
16
votes
2 answers

I can't get parameter names from valuetuple via reflection in c# 7.0

I want to Map a ValueTuple to a class using reflection. Documentation says that there is a Attribute attached to ValueTuple with parameters names (others than Item1, Item2, etc...) but I can't see any Attribute. Disassembly shows nothing. What's…
RamonEeza
  • 623
  • 6
  • 18
15
votes
2 answers

Are C# anonymous types redundant in C# 7

Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following…
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
1
2 3
9 10