title: Can't find index of an Item on a List on Silverlight
body: <p>I have a list of items of type Country and I'm trying to find the index of and specific Country on the list but the IndexOf() method always returns -1.</p>
  
  <p>The Country object look like this:</p>
  
  <pre><code>    public class Country
      {
          public string CountryCode { get; set; }
          public string CountryName { get; set; }
      }
  </code></pre>
  
  <p>Then when I try to use the IndexOf() method I do the next:</p>
  
  <pre><code>var newcountry = new Country
                       {
                           CountryCode = "VE",
                           CountryName = "VENEZUELA"
                       };
          var countries = ListBoxCountries.Items.Cast&lt;Country&gt;().ToList();
  
          if (countries.IndexOf(newcountry) == -1)
              countries.Add(newcountry);
  </code></pre>
  
  <p>Lets assume I have an already filled list with with the countries and "Venezuela" is on the list, the IndexOf() method never find the country.</p>
  
  <p>EDIT:</p>
  
  <p>So I got a little help from ReSharper here and he made this once I told him to override the Equals() method:</p>
  
  <pre><code>        public override bool Equals(object obj)
          {
              if (ReferenceEquals(null, obj)) return false;
              if (ReferenceEquals(this, obj)) return true;
              if (obj.GetType() != typeof (Country)) return false;
              return Equals((Country) obj);
          }
  
          public bool Equals(Country other)
          {
              if (ReferenceEquals(null, other)) return false;
              if (ReferenceEquals(this, other)) return true;
              return Equals(other.CountryCode, CountryCode) &amp;&amp; Equals(other.CountryName, CountryName);
          }
  
          public override int GetHashCode()
          {
              unchecked
              {
                  return ((CountryCode != null ? CountryCode.GetHashCode() : 0)*397) ^ (CountryName != null ? CountryName.GetHashCode() : 0);
              }
          }
  </code></pre>
  
  <p>And here comes another question: It's ok to do all this just to compare two objects?</p>