Search code examples
c#tuplesargument-unpacking

How to unpack a tuple right from out parameter?


Let's say I have a dictionary with points. So I could then write:

if (dict.TryGetValue(key,out (double x,double y) point))
  point.x ...

But how do get rid of point and unpack the elements of the tuple right away? I have something like this in mind:

if (dict.TryGetValue(key,out (out double x,out double y)))
  x ...

Solution

  • It seems currently it is not possible based on this opened issue.

    In terms of deconstructing the best option, I think, you have is to do it in two steps:

    if (dict.TryGetValue(key, out var point))
    {
        (double x, double y) = point;
        ...
    }