To better understand the concept of operators introduced by Mytour above, consider the following example:
Example: 2 + 3, where + is the operator used to perform addition, and 2 and 3 are operands.
For more insights into operators in C#, check out the following article by Mytour.
Exploring Operators in C#
Article Contents:
1. Operators in C#.
2. Assignment operators in C#.
3. Arithmetic operators in C#.
4. Relational operators in C#.
5. Logical operators in C#.
6. Unary operators in C#.
7. Ternary operators in C#.
8. Bitwise and Bit Shift operators in C#.
9. Mixed operators in C#.
1. Operators in C#
Operators are used to manipulate variables and values in a program. C# supports various operators classified based on the operations they perform.
Below provides details and examples of various types of operators in C#.
2. Assignment Operators in C#
Assignment operators in C# (=) are used to assign values to variables.
Example 1:
double x;
x = 50.05;
In the example above, 50.05 is assigned to X.
Example 2:
Here comes another illustration of assignment operators in C# programming language.
Upon executing the program, the output will appear as follows:
The aforementioned is a simple example demonstrating the usage of assignment operators in C#.
In the above example, you can observe the curly braces { } being utilized. Mytour will further introduce you to the usage of { } braces in string formatting. A small note is that {0} gets replaced by the first variable after the string, {1} gets replaced by the second variable, and so forth.
3. Arithmetic Operators in C#
Arithmetic operators are used to perform operations such as addition, subtraction, multiplication, division, ... .
Example 1:
Here's a simple example demonstrating arithmetic operators in C#:
int x = 5;
int y = 10;
int z = x + y;// z = 15
Below is a table listing arithmetic operators in C#:
Operator Operator Name Example
+ Addition operator 6 + 3 returns result 9
- Subtraction operator 10 - 6 returns result 4
* Multiplication operator 4 * 2 returns result 8
/ Division operator 10 / 2 returns result 2
% Modulus operator (remainder) 16 % 3 returns result 1
Example 2:
When running the program, the output will appear as:
Arithmetic operators are performed in the above example. Variables can be replaced by constants in statements.
Example:
result = 4.5 + 2.7 ; // result holds 7.2
result = firstNumber - 3.2; // result holds 11.2
4. Relational Operators in C#
Relational operators in C# are used to check the relationship between two operands. If the relationship is true, the result returns True; otherwise, it returns False.
The relational operators are used in conditional statements and loops. Below is a table listing relational operators in C#:
Operator Operator Name Example:
== Equal to (equivalent) 6 == 4, returns False
> Greater than 3 > -1, returns True
< Less than 5 < 3, returns False
>= Greater than or equal to 4 >= 4, returns True
<= Less than or equal to 5 <= 3, returns False
!= Not equal to 10 != 2, returns True
Example of relational operators in C#:
Upon executing the program, the output will be as follows:
5. Logical Operators in C#
Logical operators in C# are used to perform logical operations like AND or OR. Logical operators operate on boolean expressions (True and False) and return boolean values. They are used in decision-making and loops.
Below is a summary table of evaluated results of logical operators AND and OR:
Operand 1 Operand 2 OR (||) AND (&&)
True True True True
True False True False
False True True False
False False False False
operator in c 10
In the summary table above:
- If any of the operands is True, the OR operator evaluates to True.
- If any of the operands is False, the AND operator evaluates to False.
Example of logical operators in C#:
When running the program, the output will be as follows:
True
False
6. Unary Operators in C#
Unlike other operators, Unary operators work on a single operand.
Below is a table listing unary operators in C#:
Operator Operator Name Description:
+ Unary Plus Keeps the sign of the operand
- Unary Minus Reverses the sign of the operand
++ Increment Increases the value by 1
-- Decrement Decreases the value by 1
! Logical Negation (Not) Inverts the boolean value
Example of Unary Operators in C#:
Upon executing the program, the output will be as follows:
The operators (++) and (--) can be used as prefix and postfix. When used as a prefix, the variable's value change is seen on the same line, and when used as a postfix, the variable's value change is seen on the next line.
For better understanding, readers can refer to the example below:
Example of (++) and (--) operators in C#:
Upon executing the program, the output will be as follows:
In the above example, you can see the ++ operator being used as a postfix, and used after the operand, the value is evaluated first and then incremented by 1. So the statement:
Console.WriteLine((number++));
Prints 10 instead of 11. After being printed, the value of number is incremented by 1.
Conversely, when ++ is used as a prefix, the value is incremented before printing. Hence the statement:
Console.WriteLine((++number));
prints 12.
Similar applies for the -- operator.
7. Ternary Operator in C#
The ternary operator ? : in C# operates on 3 operands. It's a shorthand form of the if-then-else statement. The ternary operator can be used as follows:
Variable (variable) = Condition (condition)? Expression1 (expression 1) : Expression2 (expression 2);
The ternary operator works as follows:
- If the condition expression evaluates to True, the result of expression 1 (Expression1) is assigned to the variable.
- If False, the result of expression 2 (Expression2) is assigned to the variable.
Example of ternary operator in C#:
Upon executing the program, the output will be:
10 is Even Number
8. Bitwise and Bit Shift Operators in C#
Bitwise and Bit Shift operators in C# are used to perform bitwise operations.
Below is a table listing Bitwise and Bit Shift operators in C#:
Operator Operator Name
- Bitwise complement operator
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise Exclusive OR operator
< Bitwise Left Shift operator
>> Bitwise Right Shift operator
Example of Bitwise and Bit Shift operators in C#:
When running the program, the output is as follows:
9. Mixed Operators in C#
Table listing mixed operators in C#:
Operator Operator Name Example Returned Result
+= Addition Assignment x += 5 x = x + 5
-= Subtraction Assignment x -= 5 x = x - 5
= Multiplication Assignment x = 5 x = x * 5
/= Division Assignment x /= 5 x = x / 5
%= Modulo Assignment x %= 5 x = x % 5
&= Bitwise AND Assignment x &= 5 x = x & 5
|= Bitwise OR Assignment x |= 5 x = x | 5
^= Bitwise XOR Assignment x ^= 5 x = x ^ 5
<= Left Shift Assignment x <= 5 x = x << 5
>>= Right Shift Assignment x >>= 5 x = x >> 5
=> Lambda Operator x => xx Returns xx
Example of Mixed Operators in C#:
When executing the program, the output appears as:
This article from Mytour has just introduced you to operators in C#. Additionally, readers can explore more C# tutorials on Mytour to learn more about constants and how to use constants in C# .