The simple expressions are literals such as integer and real numbers and names of variables.
The Expression
class provides an Evaluate
method to evaluate your expressions at run-time. The following example evaluates a simple mathematical expression.
public static void Example1()
{
string expression = "1 + 2";
var exprObj = new Expression(expression);
var result = exprObj.Evaluate();
Console.WriteLine("{0} = {1}", expression, result);
}
The Evaluate
method evaluates the string passed as a parameter and returns its resulting value.
You can also get the strongly-typed evaluation by using the generic version of the same method as shown below.
public static void Example2()
{
string expression = "1 + 2";
var exprObj = new Expression(expression);
int result = exprObj.Evaluate<int>();
Console.WriteLine("{0} = {1}", expression, result);
}
In an expression with multiple operators, the operators with higher precedence are evaluated before the operators with lower precedence.
In the following example, multiplication is performed first because it has higher precedence than addition.
public static void Example3()
{
string expression = "1 + 2 * 3";
var exprObj = new Expression(expression);
var result = exprObj.Evaluate();
Console.WriteLine("{0} = {1}", expression, result);
}
You can use the parentheses to change the order of evaluation imposed by operator precedence.
public static void Example4()
{
string expression = "(1 + 2) * 3";
var exprObj = new Expression(expression);
var result = exprObj.Evaluate();
Console.WriteLine("{0} = {1}", expression, result);
}