10.1 Operators and their precedence in C


10.1 Operators and their precedence in C#

Table 10.1. C# operators in decreasing order of precedence

Higher precedence

Operator category

Operators

Primary

x.y f(x) a[x] x++ x-- new typeof checked unchecked

Unary

+ “ ! ~ ++ x --x (T)x

Multiplicative

* / %

Additive

+ -

Shift

<< >>

Relational and type testing

< > < = > = is as

Equality

== ! =

Logical AND

&

Logical XOR

^

Logical OR

Conditional AND

&&

Conditional OR

Conditional (ternary)

?:

Assignment

= * = / = % = += - = << = >> = & = ^ = =

All operators in the same category have the same precedence. To decide which operator in a particular group takes precedence, follow these associative rules:

  • except for the assignment operators, all other binary operators are left associative “ for example, a+b-c is the same as (a+b)-c ;

  • the assignment operators and conditional (ternary) operator are right associative “ for example, if a , b , and c are non-boolean variables of the same type, a=b=c is the same as a=(b=c) [1] ;

    [1] The value of c is assigned to b . This is followed by the value of b being assigned to a .

  • you can use brackets to enforce precedence. [2]

    [2] Please do use brackets to make your codes look clearer if you are using multiple operators in a single expression. Expressions containing many operators are very difficult to read and, if there are no brackets to specify precedence, it becomes necessary to refer to the precedence table.

Each operator is described in Table 10.2:

Table 10.2. C# operators with brief descriptions “ operators which require special attention for Java developers are shaded and are detailed in later sections

Category

Operator

Comments

Primary

x.y

Dot operator [1]

Used to specify the member of an object/class “ x is the object, y is the member

f(x)

Used to list the arguments or parameters to a method “ f is the method name , and x represents the method parameter(s)

a[x]

Used to index an array or an C# indexer (see Chapter 21)

x++

Post increment operator

Same as Java

x--

Post decrement operator

Same as Java

new

new operator

Used to create an instance of a class “ besides being an operator keyword, note that the new keyword can also be used as a modifier in C# for name hiding (see section 7.11)

typeof

typeof operator

Returns the System.Type object representing the type of the object it is applied to “ similar to java.lang.Object 's getClass() method

checked

checked operator

Control overflow checking for mathematical operations “ no equivalent in Java

unchecked

unchecked operator

Control overflow checking for mathematical operations “ no equivalent in Java

Unary

+

Unary plus

Same as Java “ example: +3

-

Unary minus

Same as Java “ example: -3

!

(Boolean) logical negation operator

Only to be used on boolean values “ returns true or false only.

Same as Java's ! operator

~

Bitwise complement operator

Only to be used on the following C# simple types “ int , uint , long , ulong .

Same as Java's ~ operator

++x

Prefix increment operator

Same as Java

--x

Prefix decrement operator

Same as Java

(T)expression

Cast operator

Used to explicitly cast an expression into another type “ T denotes the new type.

Same as Java

Multiplicative

*

Mathematical multiplication (also used for dereferencing pointers in unsafe codes)

Same as Java

/

Mathematical integral division

Same as Java

%

Mathematical remainder ( modulus )

Same as Java

Additive

+

Mathematical addition (binary add)

Same as Java

-

Mathematical subtraction (binary minus)

Same as Java

Shift

<<

(Bitwise) Left-shift operator

Same as Java

>>

(Bitwise) Right-shift operator [2]

Same as Java

Relational and type testing

<

Smaller-than operator

Same as Java

>

Bigger-than operator

Same as Java

< =

Smaller-than or equals operator

Same as Java

> =

Bigger-than or equals operato

Same as Java

is

is operator

Used to check if an object is of a particular type “ similar to Java's instanceof operator

as

as operator

Can be viewed as a combination of the is operator and a cast “ no equivalent in Java

Equality

==

Equality operator

Slightly different from Java's == when used to compare string objects “ otherwise very similar to Java's == operator

! =

Inequality operator

Because the == operator behaves slightly differently in C# and Java, the ! = operator, which returns the complementary result of the == operator is also different “ returns the complementary result of the == operator

Bitwise/boolean AND

&

&-operator

Like Java, can be used for both bool (boolean AND) and integral (bitwise AND) types

Bitwise/boolean XOR

^

XOR operator

Like Java, can be used for both bool (boolean AND) and integral (bitwise AND) types

Bitwise/boolean OR

operator

Like Java, can be used for both bool (boolean AND) and integral (bitwise AND) types

Conditional AND (also known as short-circuit boolean AND)

&&

Conditional & operator

Similar to Java's && operator “ will only evaluate second operand if first operand is true. Like Java's && , this operator applies only for bool types (not for integral types)

Conditional OR (also known as short-circuit boolean OR)

Conditional operator

Similar to Java's operator “ will not evaluate second operand if first operand is true. Like Java's , this operator applies only for bool types (not for integral types)

Conditional

?:

Conditional operator

Only operator which requires three operands (hence, also popularly known as the 'ternary operator') “ same as Java's ternary operator

Assignment [3]

=

Assignment operator

Same as Java

* =

Multiplication assignment operator

Same as Java

/ =

(Integral) Division assignment operator

Same as Java

% =

Remainder assignment operator

Same as Java

+=

Addition assignment operator

Same as Java

- =

Subtraction assignment operator

Same as Java

<< =

Left shift assignment operator

Same as Java

>> =

Right shift assignment operator

Same as Java

& =

AND assignment operator

Same as Java

^ =

XOR assignment operator

Same as Java

=

OR assignment operator

Same as Java

Additional C# operators used for unsafe code (see Chapter 29):

Operators for unsafe coding

*

Pointer operator

Used to declare pointer types and for dereferencing pointers (same symbol is used as the mathematical multiplication operator) “ no equivalent in Java

&

Address of operator

Used to retrieve the address of a particular object (same symbol is used as the bitwise and boolean AND operator) “ no equivalent in Java

sizeof

sizeof operator

Returns the size (number of bytes) of a simple type or other structs “ no equivalent in Java

-

Member-of operator

Used for accessing a member of a class via pointers, like the . operator “ Can only be applied to a pointer type. No equivalent in Java

[1] C++'s :: (class reference operator) has been removed from C#. Use the dot operator instead to refer to a class member.

[2] C# has no >>> operator.

[3] The current implementation of C# converts code like j+= 2; into more efficient MSIL codes than j= j+2;. Hence if possible, use +=, -= , /= , etc. in your codes.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net