title: Why does this cause an InvalidOperationException: Out of sync?
body: <p>The goal of the following code is to have "hotkeys" (for lack of a better word). Such a hotkey is a checkbox that toggles multiple checkboxes.</p>
  
  <p>I understand that Dictionaries throw an out of sync exception when you try to change a dictionary while looping over it. But the one I'm looping over is only being read from.</p>
  
  <p><strong>I've found a solution, but am still interested in why this causes the exception.</strong></p>
  
  <pre><code>public class JointTypeSelectDisplay
  {
      public Dictionary&lt;JointType, bool&gt; SelectedJoints;  
      private Dictionary&lt;JointType, JointTypeListElement&gt; _jointToggles;
  
      private void UpdateDisplay()
      {
          foreach (var joint in SelectedJoints)
          {
              _jointToggles[joint.Key].SetState(SelectedJoints[joint.Key]);
          }
      }
  }
  
  public class JointTypeListElement
  {
      public Toggle JointToggle;
  
      public void SetState(bool active)
      {
          JointToggle.isOn = active;
      }
  }
  </code></pre>
  
  <p>EDIT: stacktrace:</p>
  
  <blockquote>
    <p>InvalidOperationException: out of sync</p>
    
    <p>System.Collections.Generic.Dictionary'2+Enumerator[Windows.Kinect.JointType,System.Boolean].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912)</p>
    
    <p>System.Collections.Generic.Dictionary'2+Enumerator[Windows.Kinect.JointType,System.Boolean].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:835)</p>
    
    <p>KinectFilterTesting.JointTypeSelectDisplay.UpdateDisplay () (at Assets/Scripts/Simon/JointTypeSelectDisplay.cs:146)</p>
    
    <p>KinectFilterTesting.JointTypeSelectDisplay.SetSelectedState (System.Collections.Generic.List'1 types, Boolean selected) (at Assets/Scripts/Simon/JointTypeSelectDisplay.cs:131)</p>
    
    <p>KinectFilterTesting.JointTypeSelectDisplay.SetRightArm (Boolean selected) (at Assets/Scripts/Simon/JointTypeSelectDisplay.cs:160)</p>
    
    <p>UnityEngine.Events.InvokableCall'1[System.Boolean].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:141)</p>
    
    <p>UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)</p>
    
    <p>UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)</p>
    
    <p>UnityEngine.Events.UnityEvent'1[System.Boolean].Invoke (Boolean arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53)</p>
    
    <p>UnityEngine.UI.Toggle.Set (Boolean value, Boolean sendCallback) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:167)</p>
    
    <p>UnityEngine.UI.Toggle.Set (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:142)</p>
    
    <p>UnityEngine.UI.Toggle.set_isOn (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:136)</p>
    
    <p>UnityEngine.UI.Toggle.InternalToggle () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:199)</p>
    
    <p>UnityEngine.UI.Toggle.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:210)</p>
    
    <p>UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)</p>
    
    <p>UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction'1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)</p>
    
    <p>UnityEngine.EventSystems.EventSystem:Update()</p>
  </blockquote>