Parenthesis Expressions

Any expression can be enclosed inside a pair of parenthesis ((...)), as needed. Parenthesis do not change the value or behavior of the expression they enclose, but the can provide readability and – in certain cases – help with code ambiguity and/or override Operator precedence. The latter is especially helpful when using Binary Operators.

At best, surrounding parenthesis have no effect at all:

var x := (3 + 2);

However, they can add to the readability of more complex expressions. In the following example, both statements evaluate the same (because * has precedence over +) – but while the added parenthesis add nothing to code flow, they make the expression more readable to the developer by emphasizing that the second part executes first. In less trivial examples, that can be very helpful.

var x := 3 + 2 * 5;
var y := 3 + (2 * 5);

Parenthesis can also override operator precedence, by clearly grouping a subset of a complex set of expressions. In the following example, the parenthesis group 3 + 2 together to make sure they are seen as one expression to be evaluated first, before multiplying with 5.

var x := 3 + 2 * 5;
var y := (3 + 2) * 5;

Parenthesis can also be necessary (or at least helpful) to resolve otherwise ambiguous code – whether it is ambiguous to the compiler or (like in the above examples) merely to the reader:

var x := (y as String).Length;
var c := (new MyClass).SomeMethod();

See Also