title: C# 4.0 - Multidimensional Associative Array (or way to mimic one?)
body: <p>I'm an experienced PHP developer transitioning to C#. At present I am working on a Windows Forms application.</p>
  
  <p>I found in my searches that C# doesn't support associative arrays in the same loose fashion PHP does. I have found info on Dictionary and something about "structs" which seem to be class objects.</p>
  
  <p>The trouble I am having is getting my head around not only an Associative array, but a multi dimensional one that I want to use for keeping multiple counts in a series of loops.</p>
  
  <p>The application is reading a text log file, searching for a predefined string, pulling out the date on that line when the string is found, and incrementing a count for that string match on that date.</p>
  
  <p>In PHP, it would be as easy as this:</p>
  
  <pre><code>// Initialize
  $count_array[$string_date][$string_keyword] = 0;
  
  ...
  
  // if string is found
  $count_array[$string_date][$string_keyword] += 1;
  
  ...
  
  // To ouput contents of array
  foreach($count_array as $date -&gt; $keyword_count_array) {
      echo $date; // output date
  
      foreach($keyword_count_array as $keyword -&gt; $count) {
          echo $keyword . ": " . $count;
      }
  }
  </code></pre>
  
  <p>It seems to be a little more involved in C# (which isn't a bad thing). I have tried using an suggestion I found on another similar question but I don't really follow how to either increment or iterate/output the contents:</p>
  
  <pre><code>// Initialize
  var count_array = new Dictionary&lt;string, Dictionary&lt;string, int&gt;&gt;();
  count_array = null;
  
  ...
  
  // if string is found - I think the second reference is supposed to be a Dictionary object??
  count_array[string_date.ToShortDateString()][string_keyword]++;
  
  ...
  
  // To ouput contents of "array"
  foreach (KeyValuePair&lt;string, Dictionary&lt;string, int&gt;&gt; kvp in exportArray)
  {
      foreach(KeyValuePair&lt;string, int&gt; kvp2 in kvp.Value) 
      {
          MessageBox.Show(kvp.Key + " - " + kvp2.Key + " = " + kvp2.Value);
      }
  }
  </code></pre>
  
  <p>Am I even on the right track? Or does someone have a better/cleaner method of mimicing the PHP code above?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>With the above C# code, I actually get an error at the "// if string is found " line. The error is "Object reference is not set to an instance of an object". I am assuming that it is because I have a string in the secound reference, not a Dictionary object. So right now, I am unsure how to increment.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Thanks everyone for your time. Current code is now functional thanks to understanding how Dictionary's work. However all advice regarding the use of classes and objects for this situation is not lost either. I may refactor to suit.</p>