1
\$\begingroup\$

I've tried with:

dictionary.TryGetValue("MoreDetails", out bool MoreDetails);

but if the key is not present it defaults to false, and I need it to default to true. Else I have this working implementation:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    bool Summary = true;
    bool Backlog = true;
    bool MoreDetails = true;

    if (dictionary != null)
    {
        Summary = dictionary.ContainsKey("Summary") ? dictionary["Summary"] : true;
        Backlog = dictionary.ContainsKey("Backlog") ? dictionary["Backlog"] : true;
        MoreDetails = dictionary.ContainsKey("MoreDetails") ? dictionary["MoreDetails"] : true;
    };

    return new Dictionary<string, bool> {
        { "Summary", Summary},
        { "Backlog", Backlog},
        { "MoreDetails", MoreDetails}
    };
}

Is there a better approach? I don't like to repeat the true assignment, by the way.

\$\endgroup\$
  • \$\begingroup\$ TryGetValue will return true or false if the key exist. You should just check what's the return value of that and default the value if it returns false. \$\endgroup\$ – CharlesNRice Nov 5 at 15:22
  • \$\begingroup\$ @CharlesNRice Something like this?: dictionary.TryGetValue("MoreDetails", out bool MoreDetails) ? null : MoreDetails = true; \$\endgroup\$ – user33276346 Nov 5 at 15:31
  • \$\begingroup\$ I just posted as answer as formatting code in comments is difficult \$\endgroup\$ – CharlesNRice Nov 5 at 15:40
  • \$\begingroup\$ What about dictionary.TryGetValue(key, out value) ? value : true? \$\endgroup\$ – alexyorke Dec 3 at 23:32
1
\$\begingroup\$

Since you want to return a dictionary, you can simplify things by putting the keys in an array. Now the you can reduce the logic to one line of chained LINQ statements:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary)
{
    var keys = new string[] { "Summary", "Backlog", "MoreDetails" };
    bool value = false;
    return keys.Select(x => new { key = x, value = dictionary
                                 .TryGetValue(x, out value) ? value : true })
                                 .ToDictionary(x => x.key, x => x.value);
}

If there is a possibility of using a different set of keys you can pass the string array in to the method:

Dictionary<string, bool> CheckNullsSectionsVisibility(Dictionary<string, bool> dictionary, string[] keysToCheck)
{
    bool value = false;
    return keysToCheck.Select(x => new { key = x, value = dictionary
                                        .TryGetValue(x, out value) ? value : true })
                                        .ToDictionary(x => x.key, x => x.value);
}
\$\endgroup\$
2
\$\begingroup\$

TryGetValue returns True or False if it was found in the dictionary. I would suggest something like below. As it's still a single lookup in the dictionary to get the value but you can also set a default value other than default if it's not found in the dictionary.

bool moreDetails;
if !(dictionary.TryGetValue("MoreDetails", out moreDetails))
{
    moreDetails = true;
} 

Update to show extension method:

public static class DictionaryExtensions
{
    public static TValue TryGetWithDefaultValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
    {
        TValue value;
        if (!dictionary.TryGetValue(key, out value))
        {
            value = defaultValue;
        }
        return value;
    }
}

Then you could call it like:

var moreDetails = dictionary.TryGetWithDefaultValue("MoreDetails", true);
\$\endgroup\$
  • \$\begingroup\$ mmm if I would do this, it would become more verbose. \$\endgroup\$ – user33276346 Nov 5 at 17:27
  • \$\begingroup\$ Yes but your way you are checking the dictionary key twice. Once with contains then another time for index access. If you want you can make this an extension method so it looks cleaner \$\endgroup\$ – CharlesNRice Nov 5 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.