Επεξεργασία

Κοινή χρήση μέσω


Bicep operators

This article describes the Bicep operators. Operators are used to calculate values, compare values, or evaluate conditions. There are six types of Bicep operators:

Operator precedence and associativity

The operators below are listed in descending order of precedence (the higher the position the higher the precedence). Operators listed at the same level have equal precedence.

Symbol Type of Operation Associativity
( ) [ ] . :: Parentheses, array indexers, property accessors, and nested resource accessor Left to right
! - Unary Right to left
% * / Multiplicative Left to right
+ - Additive Left to right
<= < > >= Relational Left to right
== != =~ !~ Equality Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?? Coalesce Left to right
? : Conditional expression (ternary) Right to left

Parentheses

Enclosing an expression between parentheses allows you to override the default Bicep operator precedence. For example, the expression x + y / z evaluates the division first and then the addition. However, the expression (x + y) / z evaluates the addition first and division second.

Accessor

The accessor operators are used to access nested resources and properties on objects.

Operator Name Description
[] Index accessor Access an element of an array or property on an object.
. Function accessor Call a function on a resource.
:: Nested resource accessor Access a nested resource from outside of the parent resource.
. Property accessor Access properties of an object.

Comparison

The comparison operators compare values and return either true or false.

Operator Name Description
>= Greater than or equal Evaluates if the first value is greater than or equal to the second value.
> Greater than Evaluates if the first value is greater than the second value.
<= Less than or equal Evaluates if the first value is less than or equal to the second value.
< Less than Evaluates if the first value is less than the second value.
== Equals Evaluates if two values are equal.
!= Not equal Evaluates if two values are not equal.
=~ Equal case-insensitive Ignores case to determine if two values are equal.
!~ Not equal case-insensitive Ignores case to determine if two values are not equal.

Logical

The logical operators evaluate boolean values, return non-null values, or evaluate a conditional expression.

Operator Name Description
&& And Returns true if all values are true.
|| Or Returns true if either value is true.
! Not Negates a boolean value. Takes one operand.
?? Coalesce Returns the first non-null value.
? : Conditional expression Evaluates a condition for true or false and returns a value.

Null-forgiving

The null-forgiving operator suppresses all nullable warnings for the preceding expression.

Operator Name Description
! Null-forgiving Suppresses all nullable warnings for the preceding expression.

Numeric

The numeric operators use integers to do calculations and return integer values.

Operator Name Description
* Multiply Multiplies two integers.
/ Divide Divides an integer by an integer.
% Modulo Divides an integer by an integer and returns the remainder.
+ Add Adds two integers.
- Subtract Subtracts one integer from another integer. Takes two operands.
- Minus (unary) Multiplies an integer by -1. Takes one operand.

Note

Subtract and minus use the same operator. The functionality is different because subtract uses two operands and minus uses one operand.

Safe-dereference

The safe-dereference operator helps to prevent errors that can occur when attempting to access properties or elements without proper knowledge of their existence or value.

Operator Name Description
<base>.?<property>, <base>[?<index>] Safe-dereference Applies an object member access or an array element access operation to its operand only if that operand evaluates to non-null, otherwise, it returns null.

Spread

The spread operator is used to expand an iterable array or object into individual elements. The spread operator allows you to easily manipulate arrays or objects by spreading their elements or properties into new arrays or objects.

Operator Name Description
... Spread Allows for the expansion of iterable arrays and objects into individual elements or properties.

Next steps