title: How to properly use TryGetValue Method with a class as Key?
body: <p>My question is relative to the use of a dictionary. I will try to attach the code and then to explain my doubt:</p>
  <pre><code>    var ambassadors = new Dictionary&lt;CountryCode, Ambassador&gt;();
      Ambassador england = new Ambassador
      {
          CountryCode = new CountryCode(&quot;eng&quot;),
          Name = &quot;John&quot;,
          Age = 25
  
      };
      Ambassador australia = new Ambassador
      {
          CountryCode = new CountryCode(&quot;aus&quot;),
          Name = &quot;Martin&quot;,
          Age = 49
  
      };
  
      ambassadors.Add(england.CountryCode, england);
      ambassadors.Add(australia.CountryCode, australia);
  
  
      Console.WriteLine(&quot;Enter country code: &quot;);
      var code = Console.ReadLine();
  
      if (ambassadors.TryGetValue(new CountryCode(code), out Ambassador ambassador))
      {
          Console.WriteLine($&quot;The ambassador is {ambassador.Name}&quot;);
      }
      else
      {
          Console.WriteLine(&quot;The ambassador with the given code does not exist in the dictonary&quot;);
      }
  
      Console.ReadLine();
  }
  
  public class Ambassador
  {
      public CountryCode CountryCode { get; set; }
      public string Name { get; set; }
      public int Age { get; set; }
  }
  
  public class CountryCode
  {
      public string Code { get; }
  
      public CountryCode(string code)
      {
          Code = code;
      }
  
      public override bool Equals(object obj)
      {
          if (obj == null)
          {
              return false;
  
          }
          if (!(obj is CountryCode))
          {
              return false;
          }
          return StringComparer.OrdinalIgnoreCase.Equals(this.Code, ((CountryCode)obj).Code);
      }
      public override int GetHashCode()
      {
          return StringComparer.OrdinalIgnoreCase.GetHashCode(this.Code);
      }
  </code></pre>
  <p>My doubt is related to the line where I use the</p>
  <pre><code>ambassadors.TryGetValue
  </code></pre>
  <p>method inside the if to check the key and give me back the value.
  My question is: Why should I create a new instance (or initialize) of the Class CountrySide inside the TryGetValue?<br />
  I mean an instance of the class already exist inside ambassadors Dictionary. So, why c# ask me to initialize a new one inside the TryGetValue method and not simply check if that one specific exist, without the &quot;new&quot; before CoutryCode(code)?<br />
  If you could clarify me this concept I would appreciate it really.</p>