title: Dictionary.ContainsKey() in C# is not finding a key in the Dictionary
body: <p>I am trying to write a class for privileges/permissions.</p>
  
  <p>I am trying to use the Binary base permission System in C#.</p>
  
  <p>I got it where I believe it should work. However, it is not working as expected. </p>
  
  <p>After, I stepped thru my code I can see one issue in the method called <code>_mapPermissions</code> the line <code>if (dic.ContainsKey(vv))</code> returns false every time.</p>
  
  <p>the key is an instance of a class which have 2 values (ie. <code>secionName</code>, <code>KeyIndex</code>)</p>
  
  <p>This is my entire class</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Text.RegularExpressions;
  using MySql.Data.MySqlClient;
  
  namespace POS
  {
      class Roles
      {
  
          private Dictionary&lt;PermissionsKey, int&gt; systemPermissions = new Dictionary&lt;PermissionsKey, int&gt;();
          private Dictionary&lt;PermissionsKey, int&gt; userPermissions = new Dictionary&lt;PermissionsKey, int&gt;();
  
          public Roles()
          {
              this._getSystemPrivleges();
              this._getUserPrivleges();
  
              this._display(systemPermissions);
              this._display(userPermissions);
  
          }
  
          public bool hasAccess(string sectionName, string[] keys)
          {
  
              if (    this.systemPermissions.Count == 0
                   || this.userPermissions.Count == 0
                   || keys.Count() == 0
                   || String.IsNullOrEmpty(sectionName) 
               )
              {
                  return false;
              }
  
              int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
              int userPerm = this._mapPermissions(sectionName, keys, userPermissions);
  
  
  
              int check =  systemReq &amp; userPerm;
  
              Common.Alert("System Value" + systemReq + Environment.NewLine +"User Value " + userPerm + Environment.NewLine + "AND Results " + check);
  
  
              if (check == 0)
              {
                  return false;
              }
  
              return true;
  
          }
  
          private int _mapPermissions(string secName, string[] keys, Dictionary&lt;PermissionsKey, int&gt; dic)
          {
  
              int newKey = 0;
              var vv = new PermissionsKey();
  
              foreach (string k in keys)
              {
                  vv.sectionName = secName;
                  vv.KeyIndex = k;
  
                  if (dic.ContainsKey(vv))
                  {
                      newKey |= dic[vv] ;
                  }
              }
  
              return newKey;
          }
  
  
  
          private void _getSystemPrivleges()
          {
              var db = new dbConnetion();
  
              string sql =   " SELECT SQL_CACHE "
                           + " KeyIndex, Value, sectionName "
                           + " FROM role_keys "
                           + " WHERE status = 'active' ";
  
              foreach (var i in db.getData(sql, null, r =&gt; new SystemPermissions()
                                                             {
                                                                   pIndex = r["KeyIndex"].ToString()
                                                                 , pSec = r["sectionName"].ToString()
                                                                 , pValue = r["Value"].ToString()
                                                             }
                                             )
                      )
              {
  
                  var vv = new PermissionsKey();
                  vv.sectionName = i.pSec;
                  vv.KeyIndex = i.pIndex;
                  systemPermissions.Add(vv, Convert.ToInt32(i.pValue));
  
              }
          }
  
          private void _getUserPrivleges()
          {
              var db = new dbConnetion();
  
              string sql = " SELECT SQL_CACHE k.sectionName, k.KeyIndex, k.value "
                          +" FROM users AS su "
                          +" INNER JOIN role_relation AS r ON r.roleID = su.roleID "
                          +" INNER JOIN role_keys AS k ON k.KeyID = r.KeyID "
                          +" WHERE su.status = 'active' AND su.userID = @userid ";
  
              var parms = new List&lt;MySqlParameter&gt;();
              parms.Add(new MySqlParameter("@userid", UserInfo.UserID));
  
              foreach (var i in db.getData(sql, parms , r =&gt; new SystemPermissions()
                                                              {
                                                                    pIndex = r["KeyIndex"].ToString()
                                                                  , pSec = r["sectionName"].ToString()
                                                                  , pValue = r["Value"].ToString()
                                                              }
                                             )
                      )
              {
                  var vv = new PermissionsKey();
                  vv.sectionName = i.pSec;
                  vv.KeyIndex = i.pIndex;
  
                  userPermissions.Add(vv, Convert.ToInt32(i.pValue));
              }
          }
  
  
  
          private void _display(Dictionary&lt;PermissionsKey, int&gt; dic)
          {
              string str = "";
              foreach (KeyValuePair&lt;PermissionsKey, int&gt; d in dic)
              {
                  var vv = new PermissionsKey();
                  var c = d.Key;
  
                  str += c.sectionName + "_" + c.KeyIndex + "  =&gt;  " + d.Value.ToString() + Environment.NewLine;
  
              }
  
              Common.Alert(str);
          }
  
  
  
          private int _BinToInt(string str)
          {
              Regex binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
              int val = -1;
  
              if (binary.IsMatch(str))
              {
                  val = Convert.ToInt32(str, 2);
              }
  
              return val;
          }
  
          private string _IntToBin(int number)
          {
              return Convert.ToString(number, 2);
          }
  
  
      }
  
      class SystemPermissions{
          public string pIndex;
          public string pSec;
          public string pValue;
      }
  
      class PermissionsKey
      {
          public string sectionName;
          public string KeyIndex;
      }
  }
  </code></pre>
  
  <p>And I use this class like so</p>
  
  <pre><code>    var role = new Roles();
      string[] aa = { "view", "use" };
  
      if (role.hasAccess("system", aa))
      {
          Common.Alert("Welcome");
      }
      else
      {
          Common.Alert("NOP!");
      }
  </code></pre>
  
  <p>Note that the method <code>Common.Alert()</code> it simply display a Message (ie. <code>MessageBox</code>)</p>
  
  <p>The above code displays the following when it runs</p>
  
  <p>1) the content of the <code>systemPermissions</code> dictionary in the following order <code>PermissionsKey.sectionName</code>_<code>PermissionsKey.KeyIndex</code> => the value <code>(ie. 1 , 2, 4, 8, 16, 32, 64 .....)</code>
      system_do = > 1
      system_use => 2
      system_view => 4
      test_use => 1
      test_view => 2</p>
  
  <p>2) The content of the <code>usePermissions</code> dictionary </p>
  
  <pre><code>system_do = &gt; 1
  test_use =&gt; 1
  </code></pre>
  
  <p>The issue is that the 2 variables <code>systemReq</code>  and <code>userPerm</code> return 0 every time. I am not sure why</p>
  
  <pre><code> int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
   int userPerm = this._mapPermissions(sectionName, keys, userPermissions);
  </code></pre>