Nikolay Kostov (Nikolay.IT)
Topics covered:
- Operators in C# and Operator Precedence
- Arithmetic Operators
- Logical Operators
- Bitwise Operators
- Comparison Operators
- Assignment Operators
- Other Operators
- Implicit and Explicit Type Conversions
- Expressions
Video (in Bulgarian)
Presentation Content
What is an Operator?
- Operator is an operation performed over data at runtime
- Takes one or more arguments (operands)
- Produces a new value
- Operators have precedence
- Precedence defines which will be evaluated first
- Expressions are sequences of operators and operands that are evaluatedto a singlevalue
Operators in C#
- Operators in C# :
- Unary – take one operand
- Binary – take two operands
- Ternary ( ?: ) – takes three operands
- Except for the assignment operators, all binary operators are left-associative
- The assignment operators and the conditional operator ( ?: ) are right-associative
Categories of Operators in C#
| Category |
Operators |
| Arithmetic |
+ - * / % ++ -- |
| Logical |
&& || ^ ! |
| Binary |
& | ^ ~ << >> |
| Comparison |
== != < > <= >= |
| Assignment |
= += -= *= /= %= &= |= ^= <<= >>= |
| String concatenation |
+ |
| Type conversion |
is as typeof |
| Other |
. [] () ?: new |
Operators Precedence
| Precedence |
Operators |
| Highest |
() |
| |
++ -- (postfix) new typeof |
| |
++ -- (prefix) + - (unary) ! ~ |
| |
* / % |
| |
+ - |
| |
<< >> |
| |
< > <= >= is as |
| |
== != |
| |
& |
| |
&& |
| |
|| |
| |
?: |
| Lowest |
= *= /= %= += -= <<= >>= &= ^= |= |
Arithmetic Operators
- Arithmetic operators + , - , * are the same as in math
- Division operator / if used on integers returns integer (without rounding) or exception
- Division operator / if used on real numbers returns real number or Infinity or NaN
- Remainderoperator % returns the remainder fromdivision of integers
- The special additionoperator ++ increments avariable
Logical Operators
- Logical operatorstakeboolean operands and return boolean result
- Operator ! turns true to false and false to true
- Behavior of the operators && , || and ^ ( 1 == true , 0 == false ) :
| Operation |
|| |
|| |
|| |
|| |
&& |
&& |
&& |
&& |
^ |
^ |
^ |
^ |
| Operand1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
| Operand2 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
| Result |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
Bitwise Operators
- Bitwise operator ~ turns all 0 to 1 and all 1 to 0
- Like ! for boolean expressions but bit by bit
- The operators | , & and ^ behave like || , && and ^ for boolean expressions but bit by bit
- The << and >> move the bits (left or right)
- Behavior of the operators | , & and ^ :
- Bitwise operators are used oninteger numbers ( byte , sbyte , int , uint , long , ulong )
- Bitwise operators are applied bit by bit
| Operation |
| |
| |
| |
| |
& |
& |
& |
& |
^ |
^ |
^ |
^ |
| Operand1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
| Operand2 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
| Result |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
Comparison Operators
- Comparison operators are used to comparevariables
- == , < , > , >= , <= , !=
- Comparison operators example:
Assignment Operators
- Assignment operators are used to assign a value to avariable ,
- Assignment operators example:
Other Operators
- String concatenation operator + is used to concatenate strings
- If the second operand is not a string, it isconverted to string automatically
- Member access operator . is used to access object members
- Square brackets [] are used with arrays indexers and attributes
- Parentheses ( ) are used to overridethe default operator precedence
- Class cast operator (type) is used to cast one compatible type to another
- Conditional operator ?: has theform
- (if b is true thenthe result is x elsethe result is y )
- The new operator is used to create new objects
- The typeof operator returns System.Type object (the reflection of a type)
- The is operator checks if an object is compatible with given type
- Null-coalescing operator ?? isused to define a default value forbothnullable value typesand reference types
- Itreturns the left-hand operand if it is notnull
- Otherwiseit returns the rightoperand
Implicit Type Conversion
- Implicit type conversion
- Automatic conversion of value of onedata typeto value ofanother data type
- Allowedwhen no loss of data ispossible
- “Larger” types can implicitly take values of smaller “types”
Explicit Type Conversion
- Explicit type conversion
- Manual conversion of a value ofone datatype to a value ofanother data type
- Allowed only explicitlyby (type) operator
- Required when there is a possibility of loss ofdata or precision
Expressions
- Expressions are sequencesof operators, literals and variables thatareevaluated to somevalue
- Expressions have:
- Type (integer, real, boolean, ...)
- Value
Summary
- Wediscussed theoperators in C#:
- Arithmetic, logical, bitwise, comparison, assignment andothers
- Bitwise calculations
- Operator precedence
- We learned when to use implicit and explicit type conversions
- We learned how to use expressions
Resources
- Boolean algebra (logic)
- Bitwise mask
- Bitwise operation
- BitTwiddling Hacks