Different Operators in C# full Explanation
In C#, operators are symbols or keywords that
perform specific operations on operands (variables, constants, expressions) to
produce a result. They are used to perform arithmetic, logical, assignment,
comparison, and other operations. Here's an explanation of Different operators
in C#:
1.
Arithmetic Operators:
v Arithmetic
operators perform basic mathematical calculations.
v `+`:
Addition operator. Adds two operands.
v `-`:
Subtraction operator. Subtracts the second operand from the first operand.
v `*`:
Multiplication operator. Multiplies two operands.
v `/`:
Division operator. Divides the first operand by the second operand.
v `%`:
Modulus operator. Returns the remainder of the division.
v Example:
int a = 5;
int b = 3;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; //
Modulus
2.
Assignment Operators:
v Assignment
operators are used to assign values to variables.
v `=`:
Simple assignment operator. Assigns the value of the right operand to the left
operand.
v `+=`,
`-=`, `*=`, `/=`, `%=`: Compound assignment operators. Perform the specified
operation and assign the result to the left operand.
v Example:
int x = 10;
int y = 5;
x += y; // Equivalent to x = x +
y
3.
Comparison Operators:
v Comparison
operators are used to compare values and return a boolean result (`true` or
`false`).
v `==`:
Equality operator. Returns `true` if the operands are equal; otherwise, returns
`false`.
v `!=`:
Inequality operator. Returns `true` if the operands are not equal; otherwise,
returns `false`.
v `>`,
`<`: Greater than and less than operators. Returns `true` if the left
operand is greater/less than the right operand; otherwise, returns `false`.
v `>=`,
`<=`: Greater than or equal to, and less than or equal to operators. Returns
`true` if the left operand is greater/less than or equal to the right operand;
otherwise, returns `false`.
v Example:
int p = 5;
int q = 3;
bool isEqual = p == q; //
Equality comparison
bool isGreaterThan = p > q;
// Greater than comparison
4.
Logical Operators:
v Logical
operators are used to perform logical operations on boolean expressions.
v `&&`:
Logical AND operator. Returns `true` if both operands are `true`; otherwise,
returns `false`.
v `||`:
Logical OR operator. Returns `true` if either of the operands is `true`;
otherwise, returns `false`.
v `!`:
Logical NOT operator. Reverses the logical state of its operand.
v Example:
bool isTrue = true;
bool isFalse = false;
bool result = isTrue && isFalse; // Logical AND operation
5.
Increment and Decrement Operators:
v Increment
and decrement operators are used to increase or decrease the value of a
variable by one.
v `++`:
Increment operator. Increases the value of the operand by one.
v `--`:
Decrement operator. Decreases the value of the operand by one.
v Example:
int count = 0;
count++; // Increment by one
6.
Conditional Operator (Ternary Operator):
v The
conditional operator is used to evaluate a condition and return one of two
expressions based on the result.
v `(condition)
? trueExpression : falseExpression`
v Example:
int x = 5;
int y = (x > 0) ? 10 : -10;
// Ternary operator
These are some of the commonly used operators in C#.
Understanding and using these operators effectively will help you perform various
operations and calculations in your C# programs.
0 Comments