title: How to compile and execute a user defined formula dynamically (runtime) in c#?
body: <p>I want to write a piece of code in c# which is able to compile and execute user defined formulas entered as string (valid c# code) in a windows form. Is there a simple and elegant way for doing this?</p>
  
  <p>As an example, see the method below:</p>
  
  <pre><code>public double UserDefinedFormula(double x, double y, string str)  
  {
      double z;
  
      // BEGIN: Formula code provided by user as string
  
      if (str == "sum")
          z = x + y;
      else
          z = Math.Sqrt(x + y + 5);
  
      // END: Formula code provided by user as string
  
      return z; 
  }
  </code></pre>
  
  <p>The formula string entered by the user can contain anything provided that it is a valid c# code. In the example above, user enters following string into the text area of parameter form:</p>
  
  <pre><code>if (str == "sum")
      z = x + y;
  else
      z = Math.Sqrt(x + y + 5);
  </code></pre>