0
 string stringValue = "1+2+3";

How can I convert the above string value with operators, "1+2+3", so that it can be assigned to the intvalue as shown below in C#.

 int intValue = 1+2+3;

i.e intValue = 6;

Rav
  • 1,163
  • 4
  • 16
  • 18

3 Answers3

0

You need to compile on the fly. See for example Evaluate C# Code or Flee.

jason
  • 236,483
  • 35
  • 423
  • 525
0

Use DataTable.Compute (http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx)

  var result = new DataTable().Compute("2-3/4*12", null);

ANd this has been asked so many times before.

aiapatag
  • 3,355
  • 20
  • 24
0

I have used this library in many projects: C# calculation engine

It's very simple to use:

var ce = new CalcEngine();
double result = (double)ce.Evaluate("1+2+3");
Bidou
  • 7,378
  • 9
  • 47
  • 70