title: Validating value of a Certain key before adding it to a c# dictionary
body: <p>I am trying to implement a dictionary in such a way that It will only add in a certain value to a key if that <strong>new value</strong> is greater than the <strong>currently existing value</strong></p>
  
  <p>So the basic scenarios would be:</p>
  
  <ol>
  <li>If the key is not available, it will create a new key and a value</li>
  <li>If the key if available, it will check if the current value is greater than the value that is already assigned to that key and will only update if it is greater.</li>
  </ol>
  
  <p>Before I explain the problem I am facing here is my code</p>
  
  <pre><code>static IDictionary&lt;string, string&gt; versionStack = new Dictionary&lt;string, string&gt;();
  </code></pre>
  
  <pre><code>foreach(var item in RequiredApps) 
  {
      nameOfApp = item.Key;
      minimumVersionOfApp = item.Value.minVersion;
  
      if (versionStack.TryGetValue(nameOfApp, out minimumVersionOfApp)) 
      {
           if (Convert.ToInt32(minimumVersionOfApp) &gt;= Convert.ToInt32(item.Value.minVersion)) 
              minimumVersionOfApp = item.Value.minVersion;
      }
  
      versionStack[nameOfApp] = minimumVersionOfApp;
  }
  </code></pre>
  
  <p><strong>Note:</strong> Kindly do not worry about the <code>for</code> loop as it works fine and gives no problem there. Just want to display only the specific code that gives me problem</p>
  
  <p>Right now I have been able to fulfill the functionality to a certain level but the  problem is when the <code>TryGetValue</code> is executed it turns all my values to null.</p>
  
  <p>I am using the <code>TryGetValue</code> to see if a key has a value and  if so to retrieve it.</p>
  
  <p>I am stuck here right now and would appreciate if anyone can help show me what I am doing wrong.</p>
  
  <p><strong>Edit :-</strong></p>
  
  <p>Given that the problem I am facing is quite unclear and as suggested by Rufus L I am adding a sample dummy application with exactly the problem I am facing</p>
  
  <p>Hope this helps to clear any confusion :)</p>
  
  <pre><code>class Program
      {
  
          static IDictionary&lt;string, string&gt; versionStack = new Dictionary&lt;string, string&gt;();
  
          static string appName;
          static string minVersion;
          static void Main(string[] args)
          {
              AddOnce();
              AddTwice();
  
          }
  
          public static void AddOnce()
          {
              appName = "app01";
              minVersion = "1.0";
  
              versionStack.Add(appName, minVersion);
          }
  
          public static void AddTwice()
          {
              string existingValue;
  
              appName = "app01";
              minVersion = "3.0";
  
              if (!versionStack.TryGetValue("app01", out existingValue) || Convert.ToInt32(existingValue) &lt; Convert.ToInt32(minVersion))
              {
                  versionStack[appName] = minVersion;
              }
          }
  
      }
  </code></pre>