In this article, we will learn about C# operators, various types of C# operators with examples to demonstrate the use of the operators. Also, we will cover the reserved keywords in C# in this article.
The operators are symbols that are used to perform operations on operands. There can be many types of operations like arithmetic, logical, bitwise, etc. For example, in 10*10, *
is an operator that is used to carry out multiplication operation, while 10 is operand. Operators are used for performing operation on variables and values in a program. C# provides the following type of operators:
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Bitwise Operators
-
Assignment Operators
-
Miscellaneous Operators
1. Arithmetic Operators
These are the operators used for performing mathematical (arithmetic) operations on numbers.
Operator |
Meaning |
+ |
Used for adding two operands. |
- |
Used for subtracting two operands. |
* |
Used for multiplying two operands. |
/ |
Used for dividing the operand on the left by the operand on the right. |
% |
Used to take modulus which is nothing but the remainder value when the operand on the left is divided by the operand on the right. |
++ |
Used for incrementing the operand value by 1 |
-- |
Used to decrement the operand value by 1 |
Let's take a program example and see some of these operators in action.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
int a = 10, b = 10;
Console.WriteLine(a + b);
Console.WriteLine(a++); // increment the value by 1 (a = a + 1)
Console.WriteLine(a);
Console.ReadKey();
}
}
}
Output:
20
10
11
2. Relational Operators
These are operators used for performing relational operations on operands.
Operators |
Meaning |
== |
Used for equating two operands. Returns True if the two operands are equal, else, False. |
!= |
Used for equating two operands. Returns True if the two operands are not equal, else, False. |
> |
Used for comparing two operands. Returns True if the operand on the left side is greater than the operand on the right, else, False. |
< |
Used for comparing two operands. Returns True if the operand on the right side is greater than the operand on the left, else, False. |
>= |
Used for comparing two operands. Returns True if the operand on the left side is greater than or equal to the operand on the right, else, False. |
<= |
Used for comparing two operands. Returns True if the operand on the right side is greater than or equal to the operand on the left, else, False. |
Let's see the practical usage of some relational operators.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
int a = 0;
int b = 10;
Console.WriteLine(a > b);
Console.WriteLine(a == b);
Console.WriteLine(a < b);
Console.ReadKey();
}
}
}
Output:
False
True
False
3. Logical Operators
These are the operators used for performing logical operations on operands.
Operators |
Meaning |
& |
AND operator |
| |
OR operator |
^ |
XOR (exclusive OR) |
|| |
The || operator returns True when one (or both) of the conditions/variables (on its left and right side) is True even if the other is False. Otherwise, if both the operands are False, it returns False. |
&& |
The && operator returns True when both the conditions or variables(on its left and right side) are True. Otherwise, it returns False. |
! |
NOT operator negates the value of the operand. If the operand is True, then using the NOT operator with the operand will return False and vice versa. |
Let's see the practical usage of some logical operators.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
bool a = true, b = false;
Console.WriteLine(!b);
Console.WriteLine(a || b);
Console.WriteLine(a && b);
Console.ReadKey();
}
}
}
Output:
True
True
False
4. Bitwise Operators
These are operators used for performing bitwise operations on variables.
Operators |
Meaning |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR (XOR) |
>> |
Shift right |
<< |
Shift left |
~ |
One's complement (unary NOT) |
Let's see the practical usage of some bitwise operators.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
int a = 5, b = 3;
Console.WriteLine(a | b);
Console.WriteLine(a & b);
Console.ReadKey();
}
}
}
Output:
7
1
Code Explanation:
0000 0101 (5 in Binary)
0000 0011 (3 in Binary)
__________________________
0000 0111 (Bitwise OR = 7)
0000 0001 (Bitwise AND = 1)
5. Assignment Operators
Following are the assignment operators in C# programming language:
Operator |
Meaning |
= |
Simple assignment |
+= |
Add and assign |
-= |
Subtract and assign |
*= |
Multiply and assign |
/= |
Divide and assign |
%= |
Modulus and assign |
<<= |
Left shift and assign |
>>= |
Right shift and assign |
&= |
Bitwise and assign |
^= |
Bitwise exclusive OR and assign |
|= |
Bitwise inclusive OR and assign |
Let's see the practical usage of some assignment operators.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
int a = 10, b = 50;
a += b; //a = a + b; 10 + 50
Console.WriteLine(a);
a *= b; //a = a * b; 60 * 50
Console.WriteLine(a);
Console.ReadKey();
}
}
}
Output:
60
3000
6. Miscellaneous Operators
Following are the miscellaneous operators in C# programming language:
Operator |
Meaning |
sizeof() |
Returns the size of the data type |
typeof() |
Returns the type of a class |
& |
Returns the address of a variable |
* |
Pointer to a variable |
?: |
Conditional expression |
is |
Check an object is of a certain type |
as |
Cast without raising an exception |
Let's see the practical usage of some assignment operators.
Filename: Program.cs
using System;
namespace Studytonight
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(sizeof(int));
Console.WriteLine(typeof(void));
Console.ReadKey();
}
}
}
Output:
4
System.Void
Operator Precedence in C#
The precedence and associativity of C# operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.
Category |
Operator |
Associativity |
Postfix |
() [] -> . ++ - - |
Left to right |
Unary |
+ - ! ~ ++ -- (type)* & sizeof |
Right to left |
Multiplicative |
* / % |
Left to right |
Additive |
+ - |
Left to right |
Shift |
<< >> |
Left to right |
Relational |
< <= > >= |
Left to right |
Equality |
== != |
Left to right |
Bitwise AND |
& |
Left to right |
Bitwise XOR |
^ |
Left to right |
Bitwise OR |
| |
Left to right |
Logical AND |
&& |
Left to right |
Logical OR |
|| |
Left to right |
Conditional |
?: |
Right to left |
Null Coalescing |
?? |
Left to right |
Assignment |
= += -= *= /= %= >>= <<= &= ^= |= |
Right to left |
Comma |
, |
Left to right |
Reserved Keywords in C#
Total of 79 keywords is predefined sets of reserved words that have special meaning in C# programming language. The keywords which are reserved cannot be used as a variable name, constant name, etc.
abstract |
as |
base |
bool |
break |
byte |
case |
catch |
char |
checked |
class |
const |
continue |
decimal |
default |
delegate |
do |
double |
else |
enum |
event |
explicit |
extern |
false |
finally |
fixed |
float |
for |
foreach |
goto |
if |
implicit |
in |
in (generic modifier) |
int |
interface |
internal |
is |
lock |
long |
namespace |
new |
null |
object |
operator |
out |
out (generic modifier) |
override |
params |
private |
protected |
public |
readonly |
ref |
return |
sbyte |
sealed |
short |
sixeof |
stackalloc |
static |
string |
struct |
switch |
this |
throw |
true |
try |
typeof |
uint |
ulong |
unchecked |
unsafe |
ushort |
using |
using static |
void |
volatile |
while |
|
Contextual Keywords in C#
Other than the above-mentioned keywords, C# has the following contextual keywords. Contextual keywords have a specific meaning in a limited program context and can be used as identifiers outside that context and they are not reserved words in C#.
add |
alias |
ascending |
async |
await |
descending |
dynamic |
from |
get |
global |
group |
into |
join |
let |
orderby |
partial |
remove |
select |
set |
value |
var |
when |
where |
yield |
|
We hope that this tutorial helped you in understanding the concept of operators, reserved keywords and contextual keywords in C# programming language and the code examples proved useful. In the next tutorial we will cover Control Statements (if-else etc) in C#.