welcome to XRM blog

Keep in touch with latest CRM/ERP articles

To remain competitive your organisation must be efficient across the business process spectrum. To do so you need to take sound decisions based on a balance between the cost and risk. To do so you will be heavily dependent on your content management in itself needs...

image
Blog

C# Operators

By Abhay Choudhary on 6/4/2021

Operators are something that used to perform operations(mathematical or logical) based on the type of operator used

Also, operators tells you about the relationship between the operands(in case of two or more).

Types:

1.    Arithmetic Operators: They are used to perform the arithmetic operations such as addition, subtraction, multiplication, division, etc.

·       Addition Operator (+)

Performs addition operation

Ex: 9+3=12

·       Subtraction Operator (-)

Performs subtraction operation

Ex: 9-3=6

·       Multiplication Operator (*)

Performs multiplication operation

Ex: 9*3=27

·       Division Operator (/)

Performs division operations and gives quotient as the output.

Ex: 9/3=3

·       Modulo Operator (%)

Performs division operation and gives remainder as the output.

Ex: 9%3=0

·       Increment Operator (++)

Increases variable value by 1

Ex: A = 10

      A++ = 11

(a) Pre Increment: It increments the value before we use it in our expression.

Ex: int a = 2;

      int b = ++a;    // b = 3

(b)         Post Increment: It increments the value after executing the expression.

Ex: int a = 2;

      int b = a++;    //b = 2            

·       Decrement Operator (--)

Decreases variable value by 1

Ex: A = 10

      A-- = 9

(a) Pre Decrement : It decrements the value before we use it in our expression.

Ex: int a = 2;

      int b = --a;    // b = 1

(b)         Post Decrement: It decrements the value after executing the expression.

Ex: int a = 2;

      int b = a--;    //b = 2            

Precedence of Arithmetic Operators

Ø Postfix/Prefix: ++, --

Ø Multiplicative: *, /, %

Ø Additive: +, -

2.    Relational Operators: They are used to determine the relation between the operands.

·       Equality Comparison (==)

It checks whether the two operands are equal or not. Results true if they are equal otherwise, false.

Ex:  A == B is false

       A == A is true

·       Inequality Operator (!=)

It checks whether the two operands are equal or not. Results true if they are not equal otherwise, false.

Ex: A!=B is true

      A!=A is false

·       Greater Than (>)

It checks whether the first operand is greater than the second operand. Results true if condition is satisfied otherwise, false.

Ex: 6>5 will return true

       5>6 will return false

·       Less Than (<)

It checks whether the first operand is smaller than the second operand. Results true if smaller otherwise, false.

Ex: 5<6 will return true

      6<5 will return false

·       Greater Than Equal To (>=)

It checks whether the first operand is greater than or equal to the second operand. Results true if condition is satisfied otherwise, false.

Ex: 1>=1 will return true

       2>=1 will return false

·       Less Than Equal To (<=)

It checks whether the first operand is smaller than or equal to the second operand. Results true if condition is satisfied otherwise, false.

       Ex: 1<=1 will also return true

              2<=1 will return false

3.    Logical Operators: They are used to perform logical operations between the operands and returns boolean value(true or false)

·       Logical AND Operator (&&)

This operator returns true value if both the conditions(statements) are true.

Ex: 1<5 && 1<10 will return true

       1<5 && 11<10 will return false

       11<10 && 1<5 will also return false

·       Logical OR Operator (||)

This operator returns true if one of the conditions(statements) is true.

Ex: 1<5 || 5>6 will return true

       1<5 || 5<6 will return false

·       Logical Not (!)

This operator reverses the result, means it returns true if the condition is false.

Ex: !(1<5) will return false

       !(1>5) will return true

4.    Bitwise Operators: They are used to perform bit manipulation operations. It uses binary representation of both operands for the operation.

·       Bitwise AND (&)

It takes 2 operands as input and performs AND operation on every bit of those 2 operands. Its result is 1 if both bits are 1.

Ex: a=0, b=1; a & b = 0

      a=1, b=1; a & b = 1

·       Bitwise OR (|)

It takes 2 operands as input and performs OR operation on every bit of those 2 operands. Its result is 1 if any one of the two bits is 1.

Ex: a=0, b=1; a | b = 1

       a=0, b=0; a | b = 0

·       Bitwise XOR (^)

It takes 2 operands as input and performs XOR operation on every bit of those 2 operands. Its result is 1 if the two bits are different.

Ex: a=0, b=1; a ^ b = 1

      a=1, b=1; a ^ b = 0

      a=0, b=0; a ^ b = 0

·       Bitwise Left Shift (<<)

It takes 2 operands as input and performs left shifting of bits on those two operands and determines the no. of places to shift.

Ex: a= 2=0100; a<<1 = 1000 = 8

·       Bitwise Right Shift (>>)

It takes 2 operands as input and performs right shifting of bits on those two operands and determines the no. of places to shift.

Ex: a = 4 = 0100; a>>1 = 0010 = 2

·       Bitwise Complement Operator (~)

It inverts the binary bits of the operand means 1 to 0 and 0 to 1.

Ex: a= 4 = 0100; ~a = 1011 = 11

5.    Assignment Operators: They are used to assign value to a variable. The left side operand of the assignment operator is the variable and the operand on its right is the value that should be of the same data type of the variable.

·       Assignment Operator (=)

It assigns value from right side operands to its left side operand.

Ex: int a; a = 5

·       Addition Assignment Operator (+=)

It performs addition operation and assigns the result to the variable.

Ex: a += 5 is equals a = a + 5

·       Subtraction AND Assignment Operator (-=)

It performs subtraction operation and assigns the result to the variable.

Ex: a -= 5 is equals a = a - 5

·       Multiplication Assignment Operator (*=)

It performs multiplication operation and assigns the result to the variable.

Ex: a *= 5 is equals a = a * 5

·       Division Assignment Operator (/=)

It performs division operation and assigns the result to the variable.

Ex: a /= 5 is equals a = a / 5

·       Modulus Assignment Operator (*=)

It performs division operation and assigns the remainder of division to the variable.

Ex: a %= 5 is equals a = a % 5

·       Left Shift Assignment Operator (<<=)

It shifts the specified no. of bits to the left and assigns the result to the variable.

Ex: a <<= 2 is equals a = a << 2

·       Right Shift Assignment Operator (>>=)

It shifts the specified no. of bits to the right and assigns the result to the variable.

Ex: a >>= 2 is equals a = a >> 2

·       Bitwise AND Assignment Operator (&=)

It performs bitwise AND operation and assigns the result to the variable.

Ex: a &= 2 is equals a = a & 2

·       Bitwise Exclusive OR Assignment Operator(^=)

It performs bitwise XOR operation and assigns the result to the variable.

Ex: a ^= 2 is equals a = a ^ 2

·       Bitwise Inclusive OR Assignment Operator(|=)

It performs bitwise OR operation and assigns the result to the variable.

Ex: a |= 2 is equals a = a | 2 

6.    Miscellaneous Operators

·       sizeof()

It returns the size of the data type.

Ex: sizeof(int) will return 4

·       typeof()

It returns the type of the class means it gives the System.Type object for a type.

Ex: typeof(double) will return System.Double

·       Address Specifier (&)

It returns the address of a variable.

Ex: &a will return the actual address of variable a.

·       Reference Operator (*)

It is used to create a pointer to a variable.

Ex: *b is a pointer to a variable.

·       Conditional Operator (?:)

It is a ternary operator. If the condition stated by the expression is true, the result will be equal to Expression1 and if it is false, result will be equal to Expression2.

Syntax: Condition ? Expression1 : Expression2

Ex: (3>5) ? Yes : No will result No

       (3<5) ? Yes : No will result Yes

Type Conversion in C#     

It is a process of converting one predefined data type to another data type.

Following are the types of conversion in C#

1.    Implicit Type Conversion: It’s conversion that is performed by the compiler without any intervention of the user. It is a conversion from smaller to larger size type.

          char->int->long->float->double

Ex: int a=9;

      double d = a;

2.     Explicit Type Conversion: It’s an explicit conversion of an operand to a specific type by the user. It is a conversion from larger type to smaller size type.

                    double ->float->long->int->char

Ex: double d = 9.78;

      Int n = (int)d;

Data Types in C#

They are means to identify the type of data and associated operations of handling it.

·       int

It stores whole numbers without decimals. It is of 4 bytes.

Ex: int number = 5

·       long

It also stores whole numbers. This data type is used when int data type is not large enough to store values. And you have to end the value of long data type with ‘L’. It is of 8 bytes.

Ex: long number = 16000000000L

·       float

It stores the fractional numbers and you have to end the value with ‘F’. It can store 6 to 7 decimal digits. It is of 4 bytes.

Ex: float number = 2.32F

·       double

It also stores fractional numbers and you have to end the value with ‘D’. It is of 8 bytes and can store up to 15 decimal digits.

Ex: double number = 13.22D

·       bool

It can only store value true or false. It is of 1 bit.

Ex: bool isMonday = true;

·       char

It is used to store a single character, enclosed within single quotes like ‘A’. It is of 2 bytes.

Ex: char Grade = ‘A’

·       string

It is used to store sequence of characters, enclosed within double quotes like “Hello”. It is of size 2 bytes per character.

Ex: string day = “Monday”

ASP.NET
C#
Blog Calendar
Blog Calendar List
2024 Nov  4  1
2024 Aug  4  1
2024 Apr  45  4
2024 Mar  123  4
2024 Feb  210  3
2024 Jan  28  7
2023 Dec  24  6
2023 Nov  330  5
2023 Oct  451  12
2023 Sep  1165  9
2023 Aug  306  6
2023 Jul  45  6
2023 Jun  26  4
2023 May  44  5
2023 Apr  64  5
2023 Mar  178  6
2023 Feb  158  5
2023 Jan  64  4
2022 Dec  95  7
2022 Nov  282  2
2022 Sep  13  1
2022 Aug  32  2
2022 Jun  11  2
2022 May  6  2
2022 Apr  12  2
2022 Mar  2  1
2022 Feb  2  1
2022 Jan  1  1
2021 Dec  4  1
2021 Nov  2  1
2021 Oct  2  1
2021 Sep  14  1
2021 Aug  49  5
2021 Jul  50  4
2021 Jun  1640  5
2021 May  39  3
2021 Apr  2195  3
2021 Mar  208  5
2021 Feb  2536  7
2021 Jan  3728  9
2020 Dec  517  7
2020 Sep  80  3
2020 Aug  765  3
2020 Jul  134  1
2020 Jun  93  3
2020 Apr  88  3
2020 Mar  19  2
2020 Feb  34  5
2020 Jan  47  7
2019 Dec  17  4
2019 Nov  37  1
2019 Jan  23  2
2018 Dec  108  4
2018 Nov  68  3
2018 Oct  18  3
2018 Sep  1217  11
2018 Aug  7  2
2018 Jun  18  1
2018 Jan  70  2
2017 Sep  588  5
2017 Aug  17  1
2017 Jul  17  2
2017 Jun  64  2
2017 May  21  1
2017 Apr  38  2
2017 Mar  138  4
2017 Feb  829  4
2016 Dec  207  3
2016 Nov  905  8
2016 Oct  317  10
2016 Sep  770  6
2016 Aug  39  1
2016 Jun  1883  6
2016 May  112  3
2016 Jan  72  2
2015 Dec  628  6
2015 Nov  4  1
2015 Oct  13  1
2015 Sep  1470  6
2015 Aug  14  1
2015 Jul  129  2
2015 Jun  11  1
2015 May  20  1
2015 Apr  30  3
2015 Mar  80  3
2015 Jan  5343  4
2014 Dec  17  1
2014 Nov  2260  4
2014 Oct  69  1
2014 Sep  107  2
2014 Aug  5319  1
2014 Jul  49  2
2014 Apr  2591  12
2014 Mar  303  17
2014 Feb  222  6
2014 Jan  1510  16
2013 Dec  21  2
2013 Nov  693  2
2013 Oct  256  3
2013 Sep  11  1
2013 Aug  40  3
2013 Jul  214  1
2013 Apr  61  6
2013 Mar  2353  10
2013 Feb  131  3
2013 Jan  350  2
2012 Nov  61  2
2012 Oct  518  10
Tag Cloud
Interested in our services? Still not sure about project details? get a quote