简体   繁体   中英

How can I Deconstruct Value Tuples that are out parameters in C# 7?

Given the following:

var dic = new Dictionary<string, (int, int)>()
{
    ["A"] = (1, 2)
};

dic.TryGetValue("A", out (int, int) value);

I can easily get the value out of the dictionary, but how can I deconstruct it to get each individual values so something like this:

dic.TryGetValue("A", out var (left, right));

dic.TryGetValue("A", out var (left, right));

This syntax is not yet supported, it may be added in future. reference


You can give tuple elements name like this.

if(dic.TryGetValue("A", out (int left, int right) t))
{
    var (left, right) = (t.left, t.right);

    // use (left, right) or directly use (t.left, t.right)
}

I couldn't think of shorter syntax, Its just matter of time. c#7 is evolving faster than before so you just have to wait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM