YouTip LogoYouTip

Csharp Operators

Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C# has a rich set of built-in operators, categorized as follows: * Arithmetic Operators * Relational Operators * Logical Operators * Bitwise Operators * Assignment Operators * Other Operators This tutorial will explain arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one. The following table shows all arithmetic operators supported by C#. Assume variable **A** has a value of 10, and variable **B** has a value of 20, then: | Operator | Description | Example | | --- | --- | --- | | + | Adds two operands | A + B will give 30 | | - | Subtracts the second operand from the first | A - B will give -10 | | * | Multiplies both operands | A * B will give 200 | | / | Divides the numerator by the denominator | B / A will give 2 | | % | Modulus Operator - gives the remainder after integer division | B % A will give 0 | | ++ | Increment operator - increases integer value by 1 | A++ will give 11 | | -- | Decrement operator - decreases integer value by 1 | A-- will give 9 | ### Example Look at the following example to understand all the available arithmetic operators in C#: ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a =21; int b =10; int c; c = a + b; Console.WriteLine("Line 1 - c value is {0}", c); c = a - b; Console.WriteLine("Line 2 - c value is {0}", c); c = a * b; Console.WriteLine("Line 3 - c value is {0}", c); c = a / b; Console.WriteLine("Line 4 - c value is {0}", c); c = a % b; Console.WriteLine("Line 5 - c value is {0}", c); // ++a performs increment first then assigns c =++a; Console.WriteLine("Line 6 - c value is {0}", c); // Now a is 22 // --a performs decrement first then assigns c =--a; Console.WriteLine("Line 7 - c value is {0}", c); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Line 1 - c value is 31Line 2 - c value is 11Line 3 - c value is 210Line 4 - c value is 2Line 5 - c value is 1Line 6 - c value is 22Line 7 - c value is 21 * **c = a++**: Assigns the value of a to c, then increments a. * **c = ++a**: Increments a first, then assigns the value of a to c. * **c = a--**: Assigns the value of a to c, then decrements a. * **c = --a**: Decrements a first, then assigns the value of a to c. ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a =1; int b; // a++ assigns first then performs increment b = a++; Console.WriteLine("a = {0}", a); Console.WriteLine("b = {0}", b); Console.ReadLine(); // ++a performs increment first then assigns a =1;// Re-initialize a b =++a; Console.WriteLine("a = {0}", a); Console.WriteLine("b = {0}", b); Console.ReadLine(); // a-- assigns first then performs decrement a =1;// Re-initialize a b= a--; Console.WriteLine("a = {0}", a); Console.WriteLine("b = {0}", b); Console.ReadLine(); // --a performs decrement first then assigns a =1;// Re-initialize a b=--a; Console.WriteLine("a = {0}", a); Console.WriteLine("b = {0}", b); Console.ReadLine(); } } } [Run Example Β»](#) Executing the above program, the output will be: a = 2 b = 1 a = 2 b = 2 a = 0 b = 1 a = 0 b = 0 The following table shows all relational operators supported by C#. Assume variable **A** has a value of 10, and variable **B** has a value of 20, then: | Operator | Description | Example | | --- | --- | --- | | == | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. | | != | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. | | > | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. | | < | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A = | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. | | <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. | ### Example Look at the following example to understand all the available relational operators in C#: ## Example using System; class Program { static void Main(string[] args) { int a =21; int b =10; if(a == b) { Console.WriteLine("Line 1 - a is equal to b"); } else { Console.WriteLine("Line 1 - a is not equal to b"); } if(a b) { Console.WriteLine("Line 3 - a is greater than b"); } else { Console.WriteLine("Line 3 - a is not greater than b"); } /* Change values of a and b */ a =5; b =20; if(a = a) { Console.WriteLine("Line 5 - b is greater than or equal to a"); } } } When the above code is compiled and executed, it produces the following result: Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is less than or equal to b Line 5 - b is greater than or equal to a The following table shows all logical operators supported by C#. Assume variable **A** holds boolean true and variable **B** holds boolean false, then: | Operator | Description | Example | | --- | --- | --- | | && | Called Logical AND operator. If both the operands are non-zero then condition becomes true. | (A && B) is false. | | || | Called Logical OR Operator. If any of the two operands is non-zero then condition becomes true. | (A || B) is true. | | ! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make it false. | !(A && B) is true. | ### Example Look at the following example to understand all the available logical operators in C#: ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { bool a =true; bool b =true; if(a && b) { Console.WriteLine("Line 1 - Condition is true"); } if(a || b) { Console.WriteLine("Line 2 - Condition is true"); } /* Change values of a and b */ a =false; b =true; if(a && b) { Console.WriteLine("Line 3 - Condition is true"); } else { Console.WriteLine("Line 3 - Condition is not true"); } if(!(a && b)) { Console.WriteLine("Line 4 - Condition is true"); } Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Line 1 - Condition is trueLine 2 - Condition is trueLine 3 - Condition is not trueLine 4 - Condition is true Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows: | p | q | p & q | p | q | p ^ q | | --- | --- | --- | --- | --- | --- | | 0 | 0 | 0 | 0 | 0 | | 0 | 1 | 0 | 1 | 1 | | 1 | 1 | 1 | 1 | 0 | | 1 | 0 | 0 | 1 | 1 | Assume A = 60 and B = 13, now in binary format, they will be as follows: A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011 The following table lists the bitwise operators supported by C#. Assume variable **A** holds 60 and variable **B** holds 13, then: | Operator | Description | Example | | --- | --- | --- | | & | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12, which is 0000 1100 | | | | Binary OR Operator copies a bit if it exists in either operand. | (A | B) will give 61, which is 0011 1101 | | ^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49, which is 0011 0001 | | ~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) will give -61, which is 1100 0011, a 2's complement of a signed binary number. | | << | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A <> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15, which is 0000 1111 | ### Example Look at the following example to understand all the available bitwise operators in C#: ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a =60;/* 60 = 0011 1100 */ int b =13;/* 13 = 0000 1101 */ int c =0; c = a & b;/* 12 = 0000 1100 */ Console.WriteLine("Line 1 - c value is {0}", c ); c = a | b;/* 61 = 0011 1101 */ Console.WriteLine("Line 2 - c value is {0}", c); c = a ^ b;/* 49 = 0011 0001 */ Console.WriteLine("Line 3 - c value is {0}", c); c = ~a;/*-61 = 1100 0011 */ Console.WriteLine("Line 4 - c value is {0}", c); c = a <>2;/* 15 = 0000 1111 */ Console.WriteLine("Line 6 - c value is {0}", c); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Line 1 - c value is 12Line 2 - c value is 61Line 3 - c value is 49Line 4 - c value is -61Line 5 - c value is 240Line 6 - c value is 15 The following table lists the assignment operators supported by C#: | Operator | Description | Example | | --- | --- | --- | | = | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C | | += | Add and assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A | | -= | Subtract and assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A | | *= | Multiply and assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A | | /= | Divide and assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A | | %= | Modulus and assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A | | <<= | Left shift and assignment operator | C <<= 2 is same as C = C <>= | Right shift and assignment operator | C >>= 2 is same as C = C >> 2 | | &= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 | | ^= | Bitwise exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 | | |= | Bitwise inclusive OR and assignment operator | C |= 2 is same as C = C | 2 | ### Example Look at the following example to understand all the available assignment operators in C#: ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a =21; int c; c = a; Console.WriteLine("Line 1 - = c value = {0}", c); c += a; Console.WriteLine("Line 2 - += c value = {0}", c); c -= a; Console.WriteLine("Line 3 - -= c value = {0}", c); c *= a; Console.WriteLine("Line 4 - *= c value = {0}", c); c /= a; Console.WriteLine("Line 5 - /= c value = {0}", c); c =200; c %= a; Console.WriteLine("Line 6 - %= c value = {0}", c); c <<=2; Console.WriteLine("Line 7 - <>=2; Console.WriteLine("Line 8 - >>= c value = {0}", c); c &=2; Console.WriteLine("Line 9 - &= c value = {0}", c); c ^=2; Console.WriteLine("Line 10 - ^= c value = {0}", c); c |=2; Console.WriteLine("Line 11 - |= c value = {0}", c); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Line 1 - = c value = 21Line 2 - += c value = 42Line 3 - -= c value = 21Line 4 - *= c value = 441Line 5 - /= c value = 21Line 6 - %= c value = 11Line 7 - <>= c value = 11Line 9 - &= c value = 2Line 10 - ^= c value = 0Line 11 - |= c value = 2 The following table lists the other important operators supported by C# including **sizeof**, **typeof**, and **? :**. | Operator | Description | Example | | --- | --- | --- | | sizeof() | Returns the size of a data type. | sizeof(int), will return 4. | | typeof() | Returns the type of a class. | typeof(StreamReader); | | & | Returns the address of a variable. | &a; will give actual address of the variable. | | * | Pointer to a variable. | *a; will point to a variable. | | ? : | Conditional Expression | If Condition is true ? Then value X : Otherwise value Y | | is | Checks if an object is of a certain type. | If( Ford is Car) // Checks if Ford is an object of the Car class. | | as | Cast without raising an exception if the cast fails. | Object obj = new StringReader("Hello"); StringReader r = obj as StringReader; | ### Example ## Example using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { /* sizeof operator example */ Console.WriteLine("Size of int is {0}", sizeof(int)); Console.WriteLine("Size of short is {0}", sizeof(short)); Console.WriteLine("Size of double is {0}", sizeof(double)); /* ternary operator example */ int a, b; a =10; b =(a ==1)?20:30; Console.WriteLine("b value is {0}", b); b =(a ==10)?20:30; Console.WriteLine("b value is {0}", b); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Size of int is 4Size of short is 2Size of double is 8 b value is 30 b value is 20 The typeof keyword is used to get the Type object for a type. It is commonly used in reflection and for dynamically creating type instances. Here is a simple example using typeof: ## Example using System; class Program { static void Main(string[] args) { Type type =typeof(string); Console.WriteLine(type.FullName); Console.ReadKey(); } } In the code above, we use the typeof keyword to get the Type object for the string type and store it in a variable `type` of type `Type`. Then, we use the FullName property to print the fully qualified name of the type. When the above code is compiled and executed, it produces the following result: System.String Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20, because operator * has higher precedence than +, so it first gets 3 * 2 evaluated as 6 and then adds 7. The following table lists the operators in order of precedence from highest to lowest. Operators with higher precedence appear at the top of the table, and those with lower precedence appear at the bottom. In an expression, higher precedence operators will be evaluated first. **Simple precedence summary**: Parentheses first, then multiplication/division, then addition/subtraction, then shifts, then relational operators, then logical operators, then conditional operator, and finally the comma operator. | 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 | | Assignment | = += -= *= /= %= >>= < b ?"a is greater than b":"a is not greater than b";// Comparison first, then conditional Console.WriteLine(result);// Outputs "a is greater than b" **Note:** Since parentheses can change operator precedence, it is recommended to use parentheses as much as possible in practical applications to clarify the order of operations and improve code readability and accuracy.
← Csharp DecisionCsharp Constants β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.