Operators

Operators

Operators

  1. Arithmetic Operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Modulus (%)
  2. Comparison Operators:
    • Equal to (==)
    • Not equal to (!=)
    • Greater than (>)
    • Less than (<)
    • Greater than or equal to (>=)
    • Less than or equal to (<=)
  3. Logical Operators:
    • AND (&&)
    • OR (||)
    • NOT (!)
  4. Assignment Operators:
    • Assign (=)
    • Add and assign (+=)
    • Subtract and assign (-=)
    • Multiply and assign (*=)
    • Divide and assign (/=)
    • Modulus and assign (%=)
  5. Bitwise Operators:
    • Bitwise AND (&)
    • Bitwise OR (|)
    • Bitwise XOR (^)
    • Bitwise NOT (~)
    • Left shift (<<)
    • Right shift (>>)
  6. Conditional Operator:
    • Conditional Operator (?:)
  7. Type Operators:
    • Type of (typeof)
    • Is (is)
    • As (as)

These are just some of the most commonly used operators in C#. There are other operators as well, but these cover a majority of use cases.

Top of Form

Example:

				
					int a = 10;
int b = 5;
int c = a + b; // c = 15
int d = a - b; // d = 5
int e = a * b; // e = 50
int f = a / b; // f = 2
int g = a % b; // g = 0
bool result1 = (a == b); // result1 = false
bool result2 = (a != b); // result2 = true
bool result3 = (a > b); // result3 = true
bool result4 = (a < b); // result4 = false

				
			
				
					bool result5 = (a >= b); // result5 = true
bool result6 = (a <= b); // result6 = false
bool a = true;
bool b = false;
bool result1 = (a && b); // result1 = false
bool result2 = (a || b); // result2 = true
bool result3 = !a; // result3 = false
int a = 10;
a += 5; // a = 15
a -= 5; // a = 10
a *= 2; // a = 20
a /= 2; // a = 10
a %= 3; // a = 1
int a = 0b1100; // a = 12
int b = 0b1010; // b = 10
int result1 = (a & b); // result1 = 8
int result2 = (a | b); // result2 = 14
int result3 = (a ^ b); // result3 = 6
int result4 = (~a); // result4 = -13
int result5 = (a << 2); // result5 = 48
int result6 = (b >> 1); // result6 = 5


				
			

These are just some examples of how to use different operators in C#. The exact use cases will depend on the specific program you’re writing.

Join To Get Our Newsletter
Spread the love