Wednesday 10 January 2018

Check for null/empty in dictionary

Dictionary<string, int> queryWhere = new Dictionary<string, int>();
            distDictionary.Add("k1", 1);
            distDictionary.Add("k2", 1000);
            distDictionary.Add("k3", 1600);


private string GetValue(string key)
{
   string returnValue;
   if(!queryWhere.TryGetValue(key, out returnValue))
   {
      returnValue= string.Empty;
   }
   return returnValue;
}

string account = GetValue("kl");
string customer = GetValue("k5");

No comments:

Post a Comment