title: C# Foreach loop in constructor iterating once
body: <p>I'm in process of making custom JSON file editor.
  In order to better manage the data I want to create a tree.
  Default JToken and JObject are too complicated to me in order to use them to manage data. That's why I want to convert JObject that is created from input file to a tree.</p>
  
  <p>The basic principle of my code is:
  You pass the JToken to the constructor of the Container object;
  Container object contains List(of a type Container)child_containers;
  If the JToken is complex not primitive it goes through all child JTokens in that
  JToken and It creates new Container with that child JToken and adds it to a child_containers list. If input JToken is primitive it saves it's value and it stops the recursion.</p>
  
  <pre><code>public class Container
  {
      protected GraphicalBlock graphicalBlock;
      //Universal usage
      protected bool IsOriginObject;
  
      //For primitive containers
      protected object PrimitiveContent;
  
      //For complex containers
      protected Container child_container;
      protected List&lt;Container&gt; child_elements;
  
      /// &lt;summary&gt;
      /// Constructor when provided with raw jobject will create a tree that allows easy data managment.
      /// Constructor is only called once, after that the whole tree will be created recusivly
      /// &lt;/summary&gt;
      /// &lt;param name="input_jobject"&gt;&lt;/param&gt;
      /// &lt;param name="Is_orig_obj"&gt;&lt;/param&gt;
      public Container(JToken input_jobject, Point loc, Form main_form_ref, bool Is_orig_obj=true)
      {
          IsOriginObject = Is_orig_obj;
  
          //Check for complex vs primitive
          if(input_jobject.HasValues)
          {
              //Populate children list with next level/tier tokens
              //Every token will execute the same procedure eventualy creating a tree
              //The main container is the origin of the tree, primitive containers are the end of the branches
              int i = 0;
              JObject job = input_jobject as JObject;
              foreach (KeyValuePair&lt;string, JToken&gt; pair in job)
              {
                  child_elements.Add(new Container(pair.Value, new Point(loc.X + i, loc.Y+120) ,main_form_ref, false));
                  i=+120;
              }
  
              graphicalBlock = new GraphicalBlock(loc, true, main_form_ref, this);
          }
          else
          {
              //If token is primitive we can extract value from it
              PrimitiveContent = input_jobject.ToObject&lt;object&gt;();
              graphicalBlock = new GraphicalBlock(loc, false, main_form_ref, this);
          }
      }
  }
  </code></pre>
  
  <p>The graphicalBlock object is just a fancy wrapper that i use for adding GUI.
  I dont think it affects the loop.</p>
  
  <p>The main problem is after first iteration and creation of first child Container the loops breaks somehow. Can somebody explain why that foreach loop breaks after first token? (No the input JToken don't contain only 1 value).</p>
  
  <p>If somebody has any questions about my code please don't hesitate.
  My problem is very specific so I couldnt find it on google or using search tool.</p>