C# Operator
Overview
Operators play a major role in programming languages, including C#. They serve as essential symbols for performing operations on operands, which can be variables or constants. For example, in the expression 2+3, the + operator performs an addition operation on the operands 2 and 3. The goal of operators is to modify variables and values inside a program. C# has a number of operators that are categorized according to the type of operations they perform.
Introduction to C# Operators
C# operators are special symbols or characters that are utilized for performing certain operations on operands, which can be variables, constants, or expressions. Within your program, C# operators allow you to manipulate and perform calculations on data. C# provides a diverse set of operators for performing different types of operations, such as arithmetic, assignment, logical, comparison, and bitwise operations.
Let's take an example to better understand the concept of C# operators.
In the above example, we have two variables, val1 and val2, both of type int (integer). We want to calculate the sum of these two numbers and store the result in the res variable. The + operator is used in this case to perform the addition operation on the operands. It adds the val1 and val2 values (which equal 27) and assigns the result to the res variable.
C# Operators Categories
C# operators can be classified into different categories based on their different functionalities. Let's go through each category one by one:
Arithmetic Operators
Arithmetic operators in C# are used for performing basic mathematical operations on all numeric type operands such as sbyte, byte, short, ushort, long, long, int, uint, float, and double.
The table below shows the arithmetic operators that C# supports. Assume variable x has a value of 5 and variable y has a value of 10, then:
Operator | Operator Name | Description | Example |
---|---|---|---|
+ | Addition | It adds two operands and returns their sum. | x+y=15 |
- | Subtraction | It subtracts the second operand from the first operand and returns the difference. | x-y=-5 |
* | Multiplication | It multiplies two operands and returns their product. | x*y=50 |
/ | Division | It divides the first operand by the second. | y/x=2 |
% | Modulus | It returns the remainder when the first operand is divided by the second. | y%x=0 |
++ | Increment | It increases the value of an operand by one. | x++ or ++x |
-- | Decrement | It decreases the value of an operand by one. | x-- or --x |
Example:
Here is a C# program that demonstrates how arithmetic operators work.
Output:
Relational Operators
Relational operators in C# are used for checking the relationship between two operands. If the relationship is true, the result of the comparison is also true. Otherwise, the result is false.
The relational operators supported by C# are listed in the table below. Assume variable x has a value of 5 and variable y has a value of 10, then:
Operator | Operator Name | Description | Example |
---|---|---|---|
== | Equal to | It checks whether the left operand is equal to the right operand. If so, it returns true. | (x == y) is not true. |
> | Greater than | It checks whether the left operand is greater than the right operand. If so, it returns true. | (x > y) is not true. |
< | Less than | It checks whether the left operand is less than the right operand. If so, it returns true. | (x < y) is true. |
>= | Greater than or equal to | It checks whether the left operand is greater than or equal to the right operand. If so, it returns true. | (x >= y) is not true. |
<= | Less than or equal to | It checks whether the left operand is less than or equal to the right operand. If so, it returns true. | (x <= y) is true. |
!= | Not equal to | It checks whether the left operand is not equal to the right operand. If so, it returns true. | (x != y) is true. |
Example:
Here is a C# program that demonstrates how relational operators work.
Output:
Logical Operators
Logical operators in C# are used to perform logical operations on Boolean expressions and evaluate their logical value.
The logical operators supported by C# are listed in the table below. Suppose we have a variable named x with a true Boolean value and another variable named y with a false Boolean value.
Operator | Operator Name | Description | Example |
---|---|---|---|
&& | Logical AND | This operator returns true if both the operands are true; otherwise, it returns false. | (x && y) is false. |
|| | Logical OR | This operator returns true if at least one of the operands is true; otherwise, it returns false. | (x || y) is true. |
! | Logical NOT | This operator is used to reverse the operand's logical state. If the operand is true, it returns true; if it is false, it returns true. | !(x && y) is true. |
Example:
Here is a C# program that demonstrates how logical operators work.
Output:
Bitwise Operators
Bitwise operators are used in C# to perform bit manipulation on individual bits of integral types.
The bitwise operators supported by C# are listed in the table below. Suppose we have a variable named x with a value of 60 and another variable named y with a value of 13.
Operator | Operator Name | Description | Example |
---|---|---|---|
& | Bitwise AND | This operator takes two numbers as operands and performs a logical AND operation on each bit of the two numbers. The result of the AND operation is 1 only when both bits are 1. | (x & y) = 12, which is 0000 1100 |
| | Bitwise OR | This operator takes two numbers as operands and performs a logical OR operation on each bit of the two numbers. The result of the OR operation is 1 only if one of the two bits is 1. | (x | y) = 61, which is 0011 1101 |
^ | Bitwise XOR | This operator takes two numbers as operands and performs a logical XOR operation on each bit of the two numbers. The result of XOR is 1 if the two bits are not the same. | (x ^ y) = 49, which is 0011 0001 |
! | Bitwise Complement | This operator performs a bitwise inversion, flipping each bit from 0 to 1 and from 1 to 0. It operates on a single integer operand. | (~x ) = -61, which is 1100 0011 in 2's complement since it is a signed binary number. |
<< | Bitwise Left Shift | This operator accepts two numbers as operands and shifts the left operand's bits to the left by a specified number of locations, filling the vacated positions with 0. The second operand decides the specified number. | x << 2 = 240, which is 1111 0000 |
>> | Bitwise Right Shift | This operator accepts two numbers as operands and shifts the left operand’s bits to the right by a specified number of locations. The second operand decides the specified number. | x >> 2 = 15, which is 0000 1111 |
Example:
Here is a C# program that demonstrates how bitwise operators work.
Output:
Assignment Operators
Assignment operators in C# are used to assign values to variables. The variable is positioned on the left side of the assignment operator, while the value is placed on the right side. It is critical to remember that the value on the right side should be of the same data type as the variable on the left side. Otherwise, an error will be generated by the compiler.
The assignment operators supported by C# are listed in the table below.
Operator | Operator Name | Description | Example |
---|---|---|---|
= | Simple Assignment | The equal symbol (=) represents the basic assignment operator. It assigns the value on the right to the variable on the left. | z = x + y assigns the value of x + y to z |
+= | Addition Assignment | This operator combines the '+' and '=' operators. It adds the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x += 2 is equivalent to x = x + 2 |
-= | Subtraction Assignment | This operator combines the '-' and '=' operators. It subtracts the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x -= 2 is equivalent to x = x - 2 |
*= | Multiplication Assignment | This operator performs a bitwise inversion, flipping each bit from 0 to 1 and from 1 to 0. It operates on a single integer operand. | x *= 2 is equivalent to x = x * 2 |
/= | Division Assignment | This operator combines the '/' and '=' operators. It divides the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x /= 2 is equivalent to x = x / 2 |
%= | Modulus Assignment | This operator combines the '%' and '=' operators. It performs a modulo operation to the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x %= 2 is equivalent to x = x % 2 |
<<= | Left Shift Assignment | This operator combines the '<<' and '=' operators. It performs a left shift operation on the current value of the variable on the left by the number of positions specified by the value on the right and then stores the result in the variable on the left. | x <<= 2 is equivalent to x = x << 2 |
>>= | Right Shift Assignment | This operator combines the '>>' and '=' operators. It performs a right shift operation on the current value of the variable on the left by the number of positions specified by the value on the right and then stores the result in the variable on the left. | x >>= 2 is equivalent to x = x >> 2 |
&= | Bitwise AND Assignment | This operator combines the '&' and '=' operators. It performs "Bitwise AND" on the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x &= 2 is equivalent to x = x & 2 |
^= | Bitwise Exclusive OR | This operator combines the '^' and '=' operators. It performs "Bitwise Exclusive OR" on the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x ^= 2 is equivalent to x = x ^ 2 |
|= | Bitwise Inclusive OR | This operator combines the '|' and '=' operators. It performs "Bitwise Inclusive OR" on the current value of the variable on the left to the value on the right and then stores the result in the variable on the left. | x |= 2 is equivalent to x = x | 2 |
Example:
Here is a C# program that demonstrates how assignment operators work.
Output:
Conditional Operator
In C#, the conditional operator, also referred to as the ternary operator, offers a concise method for expressing conditional statements. It involves three operands and assesses a Boolean condition. It has the following syntax:
In the above syntax, the condition must be evaluated as either true or false. If the condition is true, the expression1 is evaluated and becomes the result of the expression. However, if the condition is false, the expression2 is evaluated and becomes the result of the expression.
Example:
Here is a C# program that demonstrates how conditional operators work.
Output:
Conclusion
- C# operator is a special symbol or character that is utilized for performing certain operations on operands, which can be variables, constants, or expressions.
- Arithmetic C# operators are used for performing basic mathematical operations on all numeric type operands such as sbyte, byte, short, ushort, long, long, int, uint, float, and double.
- Relational C# operators are used for checking the relationship between two operands. If the relationship is true, the result of the comparison is also true. Otherwise, the result is false.
- Logical C# operators are used to perform logical operations on Boolean expressions and evaluate their logical value.
- Bitwise C# operators are used to perform bit manipulation on individual bits of integral types.
- Assignment C# operators are used to assign values to variables. The variable is positioned on the left side of the assignment operator, while the value is placed on the right side.
- In C#, the conditional operator, also referred to as the ternary operator, offers a concise method for expressing conditional statements. It involves three operands and assesses a Boolean condition.