Prefix and Postfix notation
Both the ++ and -- operator can be used as prefix or postfix operators. In prefix form:
The compiler will first increment num1 by 1 and then will assign it to num2. While in postfix form:
the compiler will first assign num1 to num2 and hen increment num1 by 1.
Assignment Operators
Assignment operators are used to assign values to variables. Common assignment operators in C# are:
The equals (=) operator is used to assign a value to an object. Like we have seen
assigns the value 'false' to the isPaid variable of Boolean type. The left hand and right hand side of the equal or any other assignment operator must be compatible, otherwise the compiler will complaint about a syntax error. Sometimes casting is used for type conversion, e.g., to convert and store a value in a variable of type double to a variable of type int, we need to apply an integer cast.
Of coerce, when casting there is always a danger of some loss of precision; in the case above, we only got the 4 of the original 4.67. Sometimes, casting may result in strange values:
Variables of type short can only task values ranging from -3768 to 32767, so the cast above can
not assign 36982 to shortValue. Hence shortValue took the last 16 bits (as a short consists of 16 bits)
of the integer 36982, which gives the value -36985 (since bit 16, which represents the value 32768 in an int, now represents -32768). If you try to cast incompatible types like:
It won't get compiled and the compiler will generate a syntax error.
Relational Operators
Relational operators are used for comparison purposes in conditional statements. Common relational
operators in c# are:
Relational operators always result in a Boolean statement; either true or false. For example if we have
two variables.
Then:
Only compatible data types can be compared. It is invalid to compare a bool with an int, so if you have