title: List.Contains<T> always giving false
body: <p>It seems that this problem has already been encountered by quite a few people:</p>
  
  <p><a href="https://stackoverflow.com/questions/2959581/list-contains-is-not-working-as-hoped">List not working as expected</a></p>
  
  <p><a href="http://bytes.com/topic/c-sharp/answers/686419-problem-using-list-contains" rel="nofollow noreferrer">Contains always giving false</a></p>
  
  <p>So I saw the answers and tried to implement the override of Equals and of GetHashCode but there seems that I am coding something wrong. </p>
  
  <p>This is the situation: I have a list of Users(Class), each user has a List and a Name property, the list property contains licenses. I am trying to do a </p>
  
  <pre><code>         if (!users.Contains(currentUser))
  </code></pre>
  
  <p>but it is not working as expected. And this is the code I did to override the Equals and GetHashCode:</p>
  
  <pre><code>    public override bool Equals(object obj)
      {
          return Equals(obj as User);
      }
  
  
      public bool Equals(User otherUser)
      {
          if (ReferenceEquals(otherUser, null))
              return false;
  
          if (ReferenceEquals(this, otherUser))
              return true;
  
          return this._userName.Equals(otherUser.UserName) &amp;&amp; 
                 this._licenses.SequenceEqual&lt;string&gt;(otherUser.Licenses);
      }
  
      public override int GetHashCode()
      {
          int hash = 13;
          if (!_licenses.Any() &amp;&amp; !_userName.Equals(""))
          {
              unchecked
              {
                  foreach (string str in Licenses)
                  {
                      hash *= 7;
                      if (str != null) hash = hash + str.GetHashCode();
                  }
                  hash = (hash * 7) + _userName.GetHashCode();
              }
          }
          return hash;
      }
  </code></pre>
  
  <p>thank you for your suggestions and help in advance!</p>
  
  <p>EDIT 1:</p>
  
  <p>this is the code where I am doing the List.Contains, I am trying to see if the list already contains certain user, if not then add the user that isn't there. The Contains only works the first time, when currentUser changes then the User inside the list changes to the current user maybe this is a problem that is unrelated to the equals, any ideas?</p>
  
  <pre><code>        if (isIn)
          {
              if (!listOfLicenses.Contains(items[3]))
                  listOfLicenses.Add(items[3]);
  
              if (!users.Contains(currentUser))
              {
                  User user2Add = new User();
                  user2Add.UserName = currentUser.UserName;
                  users.Add(user2Add);
                  userIndexer++;
              }
  
              if (users[userIndexer - 1].UserName.Equals(currentUser.UserName))
              {
                  users[userIndexer - 1].Licenses.Add(items[3]);
              }
              result.Rows.Add();
          }
  </code></pre>