Stenway Developer Network

Operators

Arithmetic Operators

Unary minus operator for signed integers:

r1 int = -i
r2 int = -(-i)

Addition, subtraction, multiplication, division and modulus (remainder) operator:

r1 int = a + b
r2 int = a - b
r3 int = a * b
r4 int = a / b
r5 int = a % b

Multiplicative operators (*, /, %) precede the additive operators (+, -).

Arithmetic Overflow and Division By Zero

OverflowError

a int = int.MaxValue + 1

DivideByZeroError

a int = 0
b int = 100/a

Bitwise Operators

And, or, exclusice or (xor)

r1 int = a & b
r2 int = a | b
r3 int = a ^ b

Complement

r int = ~a

Left shift, right shift

r1 int = a << 4
r2 int = a >> 2

Boolean Operators

And, or, exclusice or (xor)

r1 bool = a & b
r2 bool = a | b
r3 bool = a ^ b

Negation

r bool = !a

Short-circuiting

r1 bool = a && b
r2 bool = a || b

String Operators

Concatenation

r string = a + b

Comparison Operators

Equality, inequality, greater than, less than, greater than or equal, less than or equal, pointer equality, pointer inequality

r1 bool = a == b
r2 bool = a != b
r3 bool = a > b
r4 bool = a < b
r5 bool = a >= b
r6 bool = a <= b

Pointer equality, pointer inequality

r1 bool = a === b
r2 bool = a !== b

Case-insensitive (simple ordinal) string equality

a ^== b

Null-Default Operator

nullableStr string? = null
str string = nullableStr ?? "Default value"
this.doSomething(strA ?? "Default value", 123)

Conditional Operator (Ternary)

str string = i > 2 ? "a" :: "b"
this.doSomething(i > 2 ? "a" :: "b", 123)

Member Access

Member access, index operator

r1 = a.b
r2 = a[b]

Type Testing

Is

str object = "abc"
b bool = str is int

Casting

As

The as operator will always lead to a nullable type

o object = "My string"
str string? = o as string

This will lead to null:

o object = 123
str string? = o as string

Explicit Cast

o object = "My string"
str string = (string)o

Name-Of

myInt int = 123
str string = nameof(myInt)

Type-Of

type Type = typeof(MyClass)

Operator Precedence

()
a.b a[b]
+a -a !a ~a
a*b a/b a%b
a+b a-b
a<<b a>>b
a<b a>b a<=b a>=b is
a==b a!=b a===b a!==b a^==b
a&b
a^b
a|b
a&&b
a||b
a??b
a?b::c