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.
dictionary.TryGetValue(key, out value) ? value : true
? \$\endgroup\$