title: c# modifying structs in a List<T>
body: <p>Short question: How can I modify individual items in a <code>List</code>? (or more precisely, members of a <code>struct</code> stored in a <code>List</code>?)</p>
  
  <p>Full explanation:</p>
  
  <p>First, the <code>struct</code> definitions used below:</p>
  
  <pre><code>public struct itemInfo
  {
      ...(Strings, Chars, boring)...
      public String nameStr;
      ...(you get the idea, nothing fancy)...
      public String subNum;   //BTW this is the element I'm trying to sort on
  }
  
  public struct slotInfo
  {
      public Char catID;
      public String sortName;
      public Bitmap mainIcon;
      public IList&lt;itemInfo&gt; subItems;
  }
  
  public struct catInfo
  {
      public Char catID;
      public String catDesc;
      public IList&lt;slotInfo&gt; items;
      public int numItems;
  }
  
  catInfo[] gAllCats = new catInfo[31];
  </code></pre>
  
  <p><code>gAllCats</code> is populated on load, and so on down the line as the program runs.</p>
  
  <p>The issue arises when I want to sort the <code>itemInfo</code> objects in the <code>subItems</code> array.
  I'm using LINQ to do this (because there doesn't seem to be any other reasonable way to sort lists of a non-builtin type).
  So here's what I have:</p>
  
  <pre><code>foreach (slotInfo sInf in gAllCats[c].items)
  {
      var sortedSubItems =
          from itemInfo iInf in sInf.subItems
          orderby iInf.subNum ascending
          select iInf;
      IList&lt;itemInfo&gt; sortedSubTemp = new List&lt;itemInfo();
      foreach (itemInfo iInf in sortedSubItems)
      {
          sortedSubTemp.Add(iInf);
      }
      sInf.subItems.Clear();
      sInf.subItems = sortedSubTemp;   // ERROR: see below
  }
  </code></pre>
  
  <p>The error is, "Cannot modify members of 'sInf' because it is a 'foreach iteration variable'".</p>
  
  <p>a, this restriction makes no sense; isn't that a primary use of the foreach construct?</p>
  
  <p>b, (also out of spite) what does Clear() do if not modify the list? (BTW, the List does get cleared, according to the debugger, if I remove the last line and run it.)</p>
  
  <p>So I tried to take a different approach, and see if it worked using a regular for loop. (Apparently, this is only allowable because <code>gAllCats[c].items</code> is actually an <code>IList</code>; I don't think it will allow you to index a regular <code>List</code> this way.)</p>
  
  <pre><code>for (int s = 0; s &lt; gAllCats[c].items.Count; s++)
  {
      var sortedSubItems =
          from itemInfo iInf in gAllCats[c].items[s].subItems
          orderby iInf.subNum ascending
          select iInf;
      IList&lt;itemInfo&gt; sortedSubTemp = new List&lt;itemInfo&gt;();
      foreach (itemInfo iInf in sortedSubItems)
      {
          sortedSubTemp.Add(iInf);
      }
      //NOTE: the following two lines were incorrect in the original post
      gAllCats[c].items[s].subItems.Clear();
      gAllCats[c].items[s].subItems = sortedSubTemp;   // ERROR: see below
  }
  </code></pre>
  
  <p>This time, the error is, "Cannot modify the return value of 'System.Collections.Generic.IList.this[int]' because it is not a variable." Ugh! What is it, if not a variable? and when did it become a 'return value'?</p>
  
  <p>I know there has to be a 'correct' way to do this; I'm coming to this from a C background and I know I could do it in C (albeit with a good bit of manual memory management.) </p>
  
  <p>I searched around, and it seems that <code>ArrayList</code> has gone out of fashion in favor of generic types (I'm using 3.0) and I can't use an array since the size needs to be dynamic.</p>