In a programming language, operators are special symbols such as +, -, ^, etc., that perform some action on operands. The Dynamic Expresso library manages a large set of C# operators, and it also respects the C# precedence rules of operators.
For example, operators are the signs for adding, subtracting, multiplication, and division like +, -, *, /, and their operations on the integers and the real numbers.
Below is a list of the different types of operators
| Type | Operators |
|---|---|
| Arithmetic | -, +, *, /, % |
| Logical | &&, \|\|, !, ^ |
| Comparison | ==,!=, >, <, >=, <= |
| Assignment | =, |
| String Concatenation | + |
| Others | ., new, (), [], ?:, ??, typeof |
In C#, the arithmetical operators are +, -, *, etc. They perform mathematical operations such as addition, subtraction, and multiplication respectively on numerical values. The result is also a numerical value.
Here are some examples of arithmetic operators and their effects.
public static void Example1()
{
List<string> expressions = new List<string>()
{
"a + b",
"a - b",
"a * b",
"a / b",
"a % b",
};
Interpreter interpreter = new Interpreter();
interpreter.SetVariable("a", 10);
interpreter.SetVariable("b", 5);
foreach (var expression in expressions)
{
var result = interpreter.Eval(expression);
Console.WriteLine("{0} = {1}", expression, result);
}
}
Let's run the above code and you will see the following output.
a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
Logical operators or you can say Boolean operators take Boolean values and return a Boolean result (true or false).
The following table contains the logical operators in C# and the operations that they perform.
| A | B | !A | A && B | A || B | A ^ B |
|---|---|---|---|---|---|
| true | true | false | true | true | false |
| true | false | false | false | true | true |
| false | true | true | false | true | true |
| false | false | true | false | false | false |
Let's consider the following simple examples of logical operators.
public static void Example2()
{
List<string> expressions = new List<string>()
{
"a && b",
"a || b",
"!b",
"true || b",
"(5 > 7) ^ (a == b)",
};
Interpreter interpreter = new Interpreter();
interpreter.SetVariable("a", true);
interpreter.SetVariable("b", false);
foreach (var expression in expressions)
{
var result = interpreter.Eval(expression);
Console.WriteLine("{0} = {1}", expression, result);
}
}
Let's run the above code and you will see the following output.
a && b = False
a || b = True
!b = True
true || b = True
(5 > 7) ^ (a == b) = False
Comparison operators are used to comparing two or more operands. C# supports the following comparison operators.
The following example shows the usage of comparison operators.
public static void Example3()
{
List<string> expressions = new List<string>()
{
"a > b",
"a < b",
"a == b",
"a != b",
"a >= b",
"a <= b",
};
Interpreter interpreter = new Interpreter();
interpreter.SetVariable("a", 10);
interpreter.SetVariable("b", 5);
Console.WriteLine("a: {0} \t b: {1}\n", 10, 5);
foreach (var expression in expressions)
{
var result = interpreter.Eval(expression);
Console.WriteLine("{0} = {1}", expression, result);
}
}
Let's run the above code, and you will see the following output.
a: 10 b: 5
a > b = True
a < b = False
a == b = False
a != b = True
a >= b = True
a <= b = False