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: <int>, column: <int>)
, and I want to create a new tuple of the form (row: <int>, column: <int>, content: <char>)
using the previous one.
In Python I can do:
existing = [1, 2]
new = [*existing, 3]
Or with dicts:
existing = {'row':1, 'column':2}
new = {**existing, 'content':'1'}
Is there a similar pattern in C#?
I can of course deconstruct the tuple into variables, then recombine them:
var (row, column) = (row: 1, column: 2);
var result = (row, column, content: '1');
However the actual situation is more complex. I have a function that returns the existing tuple, and an one-line function that constructs the new. If I simplify this it will be like:
private (int row, int column) ExistingTuple() => (row: 1, column: 2);
private (int row, int column, char content) NewTuple() => (/* What should I do here */ExistingTuple(), content: '1');
For the simplicity and the readability, I want it as an arrow function (T F() => E;
). If I would implement my suggestion, it will be seem like:
private (int row, int column, char content) NewTuple()
{
var (row, column) = ExistingTuple();
return (row, column, content: '1');
}
Is there a more elegant way to do that?