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
15
votes
3 answers

Convert ValueTuple to IEnumerable

Is there a saner way to do the following: public static class ValueTupleAdditions { public static IEnumerable ToEnumerable(this ValueTuple tuple) { yield return tuple.Item1; yield return tuple.Item2; } public static…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
13
votes
1 answer

KeyValuePair naming by ValueTuple in C# 7

Can the new feature in C# 7.0 (in VS 2017) to give tuple fields names be translated to KeyValuePairs? Lets assume I have this: class Entry { public string SomeProperty { get; set; } } var allEntries = new Dictionary>(); // adding…
ZoolWay
  • 5,411
  • 6
  • 42
  • 76
10
votes
1 answer

Add XML documentation for named elements in ValueTuple

With ValueTuple in C# 7, it is now possible to write methods and properties that return or consume composite objects without explicitly declaring a type. These named tuples can however be potentially confusing when no documentation is provided. As…
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
10
votes
1 answer

Is the sort order of System.ValueTuple officially specified, and where?

The new ValueTuple types in C# 7 implement IComparable, but the only documentation I have been able to find on their implementation of this simply states that CompareTo's return value indicates relative position "in the sort order". It does not…
Douglas
  • 5,017
  • 1
  • 14
  • 28
10
votes
3 answers

C# ValueTuple properties naming

I'm trying ValueTuple Class in C#, and i have a doubt about properties naming, let's see: If instantiate a ValueTuple declaring the object like this: var tuple1 = (Name: "Name1", Age: 25); We can name the properties, but, like this:…
Ferus7
  • 707
  • 1
  • 10
  • 23
10
votes
4 answers

Is it possible to use ValueTuple as model in View?

Is it possible to use value tuples as model type in views in ASP.NET Core MVC? I mean like this: Controller: public IActionResult Index() { ... (int ImportsCount, int ExportsCount) importsExports = (imports.Count, exports.Count); return…
Jan Palas
  • 1,865
  • 1
  • 23
  • 35
10
votes
3 answers

C#7 value tuple/deconstruction asymmetry

Fiddle here. Given a function (string a, string b) F(), you can deconstruct the tuple it returns: var (a, b) = F(); (string c, string d) = F(); Or you can just assign it: var (a, b) e = F(); (string a, string b) f = F(); var g = F(); // One of…
8
votes
2 answers

How to deconstruction Nullable Tuple?

I am using C# 8.0 with NullableContextOptions (Nullable Reference) enabled. I have a function with this signature: public static (int line, string content)? GetNextNonEmptyLineDown(this IList contents, int fromLine) Basically, it returns…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
7
votes
1 answer

Why can ValueTuple not be const?

ValueTuple types, declared as fields, can be mutable: class Foo { (int, int) bar = (0, 1); } or readonly: class Foo { readonly (int, int) bar = (0, 1); } and this (im)mutability applies to each member. I would expect it to be stretched to…
Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
7
votes
1 answer

Why does ValueTuple use non-standard IComparable implementation?

The documentation for ValueTuple.IComparable.CompareTo(Object) says it returns: 0 if other is a ValueTuple instance; otherwise, 1 if other is null. This makes the IComparable implementation seemingly useless other than perhaps not breaking code…
Herman
  • 2,738
  • 19
  • 32
6
votes
1 answer

How does the System.ValueTuple NuGet expand my language features?

If using .Net < 4.7 tuples are not supported. If I install the NuGet "System.ValueTuple", I'm able to write code like public static (string Name, int Age) GetFoo() { return ("Bar", 99); } with .Net 4.6.2. How is that possible?…
Sven Bardos
  • 872
  • 10
  • 27
6
votes
1 answer

Value tuples expose wrong parameter name from WebAPI

I'm using web api. I've been a bit lazy and decided to return a value tuple from my controller. [HttpGet] [Route(AuthAPIRoutes.GET_MFA_DEVICES)] public (string Type, string Value)[] GetMultiFactoryMethods() { return…
johnny 5
  • 19,893
  • 50
  • 121
  • 195
6
votes
1 answer

Why does the Visual Studio watch window show wrong values for ValueTuples in a collection?

I'm finding that ValueTuples evaluate differently when I access their properties from a collection. public static List> MyTupleList = new List> { new Tuple("test", true) }; …
Rob Powell
  • 1,367
  • 2
  • 13
  • 10
6
votes
1 answer

ValueTuple does not support DisplayMemberPath. Combobox, WPF

I have a combobox and want to bind its ItemsSource to an IEnumerable<(string,string)>. If I do not set DisplayMemberPath, then it works and it shows in the dropdown area the result of calling ToString() in the items. Nevertheless when I set…
taquion
  • 2,667
  • 2
  • 18
  • 29
5
votes
2 answers

Performance penalties to deconstructing tuples in c#?

If we do var (hello, world) = GetHelloAndWorldStrings(); if (hello == "hello" && world == "world") Environment.Exit(0); Does that incur any additional costs than just doing the following: var helloAndWorld = GetHelloAndWorldStrings(); if…
SpiritBob
  • 2,355
  • 3
  • 24
  • 62
1
2
3
9 10