I'm relatively new to C# so please bear with me! The code I would prefer to write throws a null references if there is nothing in the dictionary, as it should. I'm having to cast it as string as the dictionary returns an object:
string mainDirectorsInZim = (string)zimOrganisation.OrganizationFields["main_director"];
The code I am having to write to solve that?
if (zimOrganisation.OrganizationFields.ContainsKey("main_director"))
{
mainDirectorsInZim = (string)zimOrganisation.OrganizationFields["main_director"];
}
else
{
mainDirectorsInZim = null;
}
I have not an insignificant amount of these to write, and it seems inefficient. Is there a better way to do this?