title: C# get smallest and largest values from a list
body: <p>I need to go through a list and find the largest and smallest values. The list is populated based on the input from a user entering information into a form text box. Then each time that I click on a button to calculate in my form it runs code that does calculations and puts decimals into listProfit.
  I have looked all over the place and tried using .Sort() but that causes a system error. </p>
  
  <p>This is the error I am getting. </p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException'
    occurred in mscorlib.dll Additional information: Collection was
    modified; enumeration operation may not execute.</p>
  </blockquote>
  
  <p>error highlights the "in" in my foreach loop.</p>
  
  <pre><code>//list of each order total
  List&lt;decimal&gt; listProfit = new List&lt;decimal&gt;(); 
  
      foreach(int item in listProfit)
                      {
                          listProfit.Sort();
                          decimal smallest = listProfit[0];
                          decimal largest = listProfit[listProfit.Count - 1];
                          SmallTxt.Text = smallest.ToString("c");
                          LargestTxt.Text = largest.ToString("c");
                      }
  </code></pre>