title: C# Initialize a Dictionary locally and once (static equivalent in C++)
body: <p>I have a function that instantiates and populates a Dictionary. I use the function to convert a Byte key (its parameter) into an enum value (its returned value).</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Convert the first byte of the received data from USB into a Model B event
      /// &lt;/summary&gt;
      /// &lt;param name="data"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      Model_B_Event Get_Event_From_Data(Byte[] data)
      {
          Dictionary&lt;Byte, Model_B_Event&gt; map = new Dictionary&lt;byte, Model_B_Event&gt;();
          Model_B_Event matching_event = Model_B_Event.NO_EVENT;
  
          map.Add(0, Model_B_Event.SEND_MODEL_ID);
          map.Add(5, Model_B_Event.SEND_RELAY_VOLTAGE);
          map.Add(6, Model_B_Event.SEND_SPARE_STATUS);
  
          map.TryGetValue(data[0], out matching_event);
  
          return matching_event;
      }
  </code></pre>
  
  <p>I was thinking in C++ terms and I wanted to add the static property to</p>
  
  <pre><code>static Dictionary&lt;Byte, Model_B_Event&gt; map = new Dictionary&lt;byte, Model_B_Event&gt;();
  </code></pre>
  
  <p>so that the dictionary is only built once every time the function is run. I would only check with a Boolean flag if I already built the dictionary before.</p>
  
  <p>However, the static property is not available in C# for local variables.</p>
  
  <p>Is there a way to build this dictionary only once and a way to have it only locally in the function? I don't want to share it to the class.</p>
  
  <p>Thanks.</p>