Pages

Sunday, 14 June 2015

Operators

About Operators

Precedence

The following expression:
1 + 2 * 3
it's evaluated as 1 + (2 * 3) because * has a higher precedence than +:

Left-associative operators

Binary operators (except for assignment, lambda, and null coalescing operators) are left-associative, in other words, they are evaluated from left to right. For example, the following expression:
8 / 4 / 2

Right-associative operators

The assignment operators, lambda, null coalescing, and conditional operator are right-associative; in other words, they are evaluated from right to left. Right associativity allows multiple assignments such as the following to compile:
x = y = 3;
This first assigns 3 to y, and then assigns the result of that expression (3) to x.

Operators

Following table shows you list of operator supported by C# in order of precedence
Category Operator symbol Operator name Example User-overloadable
Primary . Member access x.y No
-> (unsafe) Pointer to struct x->y No
() Function call x() No
[] Array/index a[x] Via indexer
++ Post-increment x++ Yes
-- Post-decrement x-- Yes
new Create instance new Foo() No
stackalloc Unsafe stack allocation stackalloc(10) No
typeof Get type from identifier typeof(int) No
checked Integral overflow check on checked(x) No
unchecked Integral overflow check off unchecked(x) No
default Default value default(char) No
await Await async await myTask No
Unary sizeof Get size of struct sizeof(int) No
+ Positive value of +x Yes
- Negative value of -x Yes
! Not !x Yes
- Bitwise complement -x Yes
++ Pre-increment ++x Yes
-- Pre-decrement --x Yes
() Cast (int)x No
* (unsafe) Value at address *x No
& (unsafe) Address of value &x No
Multiplicative * Multiply x * y Yes
/ Divide x / y Yes
% Remainder x % y Yes
Additive + Add x + y Yes
- Subtract x - y Yes
Shift << Shift left x >> 1 Yes
>> Shift right x << 1 Yes
Relational < Less than x < y Yes
> Greater than x > y Yes
<= Less than or equal to x <= y Yes
>= Greater than or equal to x >= y Yes
is is Type or is subclass of x is y No
as Type conversion x as y No
Equality == Equals x == y Yes
!= Not equals x != y Yes
Logical And & Logical And x & y Yes
Logical Xor ^ Exclusive Or x ^ y Yes
Logical Or | Logical Or x | y Yes
Conditional And && Conditional And x && y Via &
Conditional Or || Conditional Or x || y Via |
Null coalescing ?? Null coalescing x ?? y No
Conditional ?: Conditional isTrue ? value : otherValue No
Assignment & Lambda = Assign x = y No
*= Multiply self by x *= 2 Via *
/= Divide self by x /= 2 Via /
+= Add to self x += 2 Via +
-= Subtract from self x -= 2 Via -
<<= Shift self left by x <<= 2 Via <<
>>= Shift self right by x >>= 2 Via >>
&= And self by x &= 2 Via &
^= Exclusive-Or self by x ^= 2 Via ^
|= Or self by x |= 2 Via |
=> Lambda x => x + 1 No

0 comments:

Post a Comment