Previous Topic

Next Topic

Book Contents

Frequently used operators

For more information, see a C# or Java programming reference.

Frequently used operators

Operator category

Operator

Use

Description

Arithmetic

+

x + y

Adds x and y.

 

-

x - y

Subtracts y from x.

 

*

x * y

Multiplies x by y.

 

/

x / y

Divides x by y.

 

%

x % y

Returns the remainder of integer division of x and y.

Logical (Boolean and bitwise)

&

x & y

Evaluates both x and y and returns the logical conjunction ("AND") of their results. If x and y are integers, the logical conjunction is performed bitwise.

 

|

x | y

Evaluates x and y and returns the logical disjunction ("OR") of their results. If x and y are integers, the logical disjunction is performed bitwise.

 

^

x ^ y

Returns the exclusive or ("XOR") of their results. If x and y are integers, the exclusive or is performed bitwise.

 

!

!x

Evaluates x and returns the negation ("NOT") of the result.

  • If x evaluates to false, it returns true.
  • If x evaluates to true, it returns false.

 

~

~x

Evaluates x and returns the bitwise negation of the result. ~x returns a value where each bit is the negation of the corresponding bit in the result of evaluating x.

 

&&

x && y

Evaluates x.

  • If the result is false, it returns false.
  • Otherwise, it evaluates and returns the results of y.

Note that if evaluating y would hypothetically have no side effects, the results are identical to the logical conjunction performed by the & operator.

 

||

x || y

Evaluates x.

  • If the result is true, it returns true.
  • Otherwise, it evaluates and returns the results of y.

Note that if evaluating y would hypothetically have no side effects, the results are identical to the logical disjunction performed by the | operator.

 

( )

 

Used to group information.

String concatenation

+

x + y

Concatenates strings.

Relational

==

x == y

  • If x and y have the same value, it returns true.
  • Otherwise, it returns false.

 

!=

x != y

Returns the logical negation of the operator ==.

  • If x is not equal to y, it returns true.
  • If x is equal to y, it returns false.

 

<

x < y

  • If x is less than y, it returns true.
  • Otherwise, it returns false.

 

>

x > y

  • If x is greater than y, it returns true.
  • Otherwise, it returns false.

 

<=

x <= y

  • If x is less than or equal to y, it returns true.
  • Otherwise, it returns false.

 

>=

x >= y

  • If x is greater than or equal to y, it returns true.
  • Otherwise, it returns false.

Member access

.

this.Value

Used to form long names.

Indexing

[ ]

 

Used to access array elements.

Conditional

?:

x ? y : z

  • If x is true, it returns y.
  • Otherwise, it returns z.

Assignment

=

x = y

Assigns the value of y to x.

Send Feedback