Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

519
Views
How to read the value from a Dictionary via .TryGetValue() if the value is a tuple?

I have a dictionary of type

Dictionary<int, (float, float)>

when trying to read the value from it I can't use this way

if (myDict.TryGetValue(1, out (float tupleItem1, float tupleItem2))) { /* ... */ }

because then I get compile errors

enter image description here

The way it works is

if (myDict.TryGetValue(1, out (float, float) theTuple)) { /* ... */ }

Is there a way I can directly initialize the variables like so?

if (!myDict.TryGetValue(1, out (float tupleItem1, float tupleItem2)))
{
    /* handle if not found */
    tupleItem1 = 111;
    tupleItem2 = -12345;
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can't deconstruct directly in an out parameter yet unfortunately, see this proposal.

You'll have to deconstruct it yourself:

if (!myDict.TryGetValue(1, out var result))
{
    result = (111, -12345);
}

You can improve this situation slightly with an extension method:

public static class DictionaryExtensions
{
    public static TValue? TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key) where TValue : struct
    {
        return dict.TryGetValue(key, out var result) ? result : null;
    }
}

This lets you write:

if (myDict.TryGetValue(1) is not (float tupleItem1, float tupleItem2))
{
    tupleItem1 = 111;
    tupleItem2 = -12345;
}
over 4 years ago · Santiago Trujillo Report

0

If you find yourself doing this a lot, you could write a simple little extension method to make it more readable:

public static class DictionaryExt
{
    public static TValue TryGetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TValue> getDefault)
    {
        return dict.TryGetValue(key, out var value) 
            ? value 
            : getDefault();
    }
}

Then your sample code could looks something like this:

var dict = new Dictionary<int, (float, float)>();

var result = dict.TryGetValueOrDefault(1, () => (111, -12345));

Console.WriteLine(result);

I chose to use Func<TValue> rather than TValue for the default so that you don't have to create a default value that isn't going to be used in the case that the dictionary already contains the key.

If you want a slightly simpler syntax and you don't care that the default is created for every call regardless of whether it's actually needed you could write it like this instead:

public static class DictionaryExt
{
    public static TValue TryGetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
    {
        return dict.TryGetValue(key, out var value)
            ? value
            : defaultValue;
    }
}

Which would be called like this:

var dict = new Dictionary<int, (float, float)>();

var result = dict.TryGetValueOrDefault(1, (111, -12345));

Console.WriteLine(result);

You could of course include both methods in DictionaryExt for more flexibility.

(It's also entirely possible that the overhead of creating a Func<TValue> for every call is greater than creating a tuple for each call, so if you're worried about performance you should test it. In either case, you could pass in a precreated Func or Tuple to avoid the creation overhead.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!