title: C# Dynamic List to Static List
body: <p>From the documentation of the Event Log Class the EventLogEntryCollection is a dynamic list of event log entries. It recommends that the Count property is used directly instead of storing its value in a variable. Implementing this behavior:</p>
  
  <pre><code>private void ReadEventLog()
  {
      EventLog eventLog = new EventLog("Application", "TheGreatestMachineInTheWorld");
      EventLogEntryCollection eventLogEntries = eventLog.Entries;
      for (int i = 0; i &lt; eventLogEntries.Count; i++){
          EventLogEntry entry = eventLog.Entries[i];
          //Do Some processing on the entry
  }
  </code></pre>
  
  <p>Is slow for large event logs ( >20000 entries). The reason for the for loop instead of a foreach is because I need the iterator location to indicate how close this thing is to finishing.</p>
  
  <p>Storing the count variable and iterating over that:</p>
  
  <pre><code>int eventLogEntryCount = eventLogEntries.Count;
  for (int i = 0; i &lt; eventLogEntryCount; i++){
          EventLogEntry entry = eventLog.Entries[i];
          //Do Some processing on the entry
  }
  </code></pre>
  
  <p>Provides a noticeable performance boost. However it opens the possibility for an index out of range exception if the event log is being written to as the processing happens. Is there a way to store this list statically so the count does not change?</p>