In a programming language, operators are special symbols such as +, -, *, etc., that perform some action on operands. The Flee 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 | And, Or, Not, Xor |
| Comparison | =,<>, >, <, >=, <= |
| String Concatenation | + |
In C#, the arithmetical operators are +, -, *, etc., and they perform mathematical operations such as addition, subtraction, and multiplication respectively on numerical values, and 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",
"b / a",
"a % b"
};
ExpressionContext context = new ExpressionContext();
context.Variables["a"] = 6.5;
context.Variables["b"] = 10.0;
foreach (var expression in expressions)
{
IDynamicExpression eDynamic = context.CompileDynamic(expression);
Object result = eDynamic.Evaluate();
Console.WriteLine("{0} = {1}", expression, result);
}
}
Let's run the above code and you will see the following output.
a + b = 16.5
a - b = -3.5
a * b = 65
b / a = 1.5384615384615385
a % b = 6.5
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 And B | A Or B | A Xor B |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | true |
| false | true | false | true | true |
| false | false | false | false | false |
Let's consider the following simple examples of logical operators.
a And b = False
a Or b = True
Not b = True
true Or b = True
a Xor b = True
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",
};
ExpressionContext context = new ExpressionContext();
context.Variables["a"] = 6.5;
context.Variables["b"] = 10.0;
foreach (var expression in expressions)
{
IDynamicExpression eDynamic = context.CompileDynamic(expression);
Object result = eDynamic.Evaluate();
Console.WriteLine("{0} = {1}", expression, result);
}
}