C#/C# Tutorial

C# Operators | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 21:08
반응형

 C# Operators 

 

C# provides a variety of operators that you can use to perform operations on variables and values. 

These operators include:

  • Arithmetic operators:
    • + (addition)
    • - (subtraction)
    • * (multiplication)
    • / (division)
    • % (modulus)
    • ++ (increment)
    • -- (decrement)
  • Assignment operators:
    • = (simple assignment)
    • += (addition assignment)
    • -= (subtraction assignment)
    • *= (multiplication assignment)
    • /= (division assignment)
    • %= (modulus assignment)
  • Comparison operators:
    • == (equality)
    • != (inequality)
    • > (greater than)
    • < (less than)
    • >= (greater than or equal to)
    • <= (less than or equal to)
  • Logical operators:
    • && (logical AND)
    • || (logical OR)
    • ! (logical NOT)
  • Bitwise operators:
    • & (bitwise AND)
    • | (bitwise OR)
    • ^ (bitwise XOR)
    • ~ (bitwise NOT)
    • << (left shift)
    • >> (right shift)
  • Ternary conditional operator:
    • ? : (conditional operator), which evaluates a Boolean expression and returns one of two values depending on the result.
  • Null-coalescing operator:
    • ??, which returns the left-hand operand if it is not null, or the right-hand operand otherwise.

 

Here's an example of using some of these operators:

int a = 10;
int b = 5;
int c = a + b; // addition
c += 2; // addition assignment
bool result = (c > 10) && (a < 20); // logical AND
string message = (result ? "c is greater than 10" : "c is less than or equal to 10"); // conditional operator

In this example, the variables a and b are added together using the + operator, and the result is assigned to the variable c using the = operator. 

The += operator is then used to add 2 to the value of c. 

The logical AND operator (&&) is used to compare the values of c and a, and the result is assigned to the boolean variable result. 

Finally, the conditional operator (? :) is used to determine the value of the string variable message based on the value of result.

 

 Examples 

Here are some examples of using different operators in C#:

  1. Arithmetic operators:

int a = 10;
int b = 5;
int c = a + b; // c will be 15
int d = a - b; // d will be 5
int e = a * b; // e will be 50
int f = a / b; // f will be 2
int g = a % b; // g will be 0

 

  2. Assignment operators:

int a = 10;
a += 5; // equivalent to a = a + 5; a will be 15
a -= 3; // equivalent to a = a - 3; a will be 12
a *= 2; // equivalent to a = a * 2; a will be 24
a /= 4; // equivalent to a = a / 4; a will be 6
a %= 2; // equivalent to a = a % 2; a will be 0

 

  3.Comparison operators:

int a = 10;
int b = 5;
bool isEqual = (a == b); // isEqual will be false
bool isNotEqual = (a != b); // isNotEqual will be true
bool isGreater = (a > b); // isGreater will be true
bool isLess = (a < b); // isLess will be false
bool isGreaterOrEqual = (a >= 10); // isGreaterOrEqual will be true
bool isLessOrEqual = (b <= 4); // isLessOrEqual will be false

 

  4.Logical operators:

bool x = true;
bool y = false;
bool result1 = x && y; // result1 will be false
bool result2 = x || y; // result2 will be true
bool result3 = !x; // result3 will be false

 

  5. Bitwise operators:

int a = 12; // binary representation: 1100
int b = 7; // binary representation: 0111
int c = a & b; // c will be 4 (binary representation: 0100)
int d = a | b; // d will be 15 (binary representation: 1111)
int e = a ^ b; // e will be 11 (binary representation: 1011)
int f = ~a; // f will be -13 (binary representation: 1111 0011)
int g = a << 2; // g will be 48 (binary representation: 1100 0000)
int h = a >> 2; // h will be 3 (binary representation: 0000 0011)

 

  6. Ternary operator:

int a = 10;
int b = 5;
string message = (a > b) ? "a is greater than b" : "a is less than or equal to b";

In this example, the value of the string variable message will be "a is greater than b" if a is greater than b, and "a is less than or equal to b" otherwise.

 

  7. Null-coalescing operator:

int? a = null;
int b = a ?? 5; // b will be 5, because a is null
a = 10;
int c = a ?? 5; // c will be 10, because a is not null

In this example, the null coalescing operator is used to provide a default value for a nullable integer variable.

반응형